Jar Package
Configuration file
Xml
<servlet>
<servlet-name>MyDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--set your own defined controller XML file--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--Spring MVC configuration file Ends--
<!--intercept Settings--
<servlet-mapping>
<servlet-name>MyDispatcher</servlet-name>
<!--intercept all requests by SPRINGMVC--
<url-pattern>/</url-pattern>
</servlet-mapping>
Spring Mvc.xml
<!--upload file settings, Maxuploadsize=-1, indicates infinity. Uploadtempdir is an uploaded temp directory--
<bean id= "Multipartresolver"
class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name= "defaultencoding" value= "UTF-8" ></property>
<property name= "maxuploadsize" value= "5400000" ></property>
<property name= "Uploadtempdir" value= "Fileupload/temp" ></property>
</bean>
Front Page fileupload.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ taglib prefix= "form" uri= "Http://www.springframework.org/tags/form"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title> Upload Image Test </title>
<base href= "<%=basePath%>" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<body>
<center>
<form action= "File/upfile"
Method= "POST" enctype= "Multipart/form-data" >
<input type= "File" name= "file"/>
<input type= "Submit" value= "Upload"/>
</form>
</center>
</body>
Controller layer
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.util.UUID;
Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.ModelMap;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
@RequestMapping ("/file")
public class Filecontroller {
@RequestMapping ("/tofile")
Public String Tofileupload () {
return "FileUpload";
}
@RequestMapping ("/tofile2")
Public String toFileUpload2 () {
return "FileUpload2";
}
/**
* Upload Files
*/
@RequestMapping ("/upfile")
Public String Onefileupload (
@RequestParam ("file") Commonsmultipartfile file,
HttpServletRequest request, Modelmap model) {
Get the original file name
String fileName = File.getoriginalfilename ();
SYSTEM.OUT.PRINTLN ("Original filename:" + filename);
New file name
String NewFileName = uuid.randomuuid () + fileName;
Get the path to the project
ServletContext sc = request.getsession (). Getservletcontext ();
Upload Location
String Path = Sc.getrealpath ("/img") + "/"; Set the directory where files are saved
File F = new file (path);
if (!f.exists ())
F.mkdirs ();
if (!file.isempty ()) {
try {
FileOutputStream fos = new FileOutputStream (path + newfilename);
InputStream in = File.getinputstream ();
int b = 0;
while ((b = In.read ())! =-1) {
Fos.write (b);
}
Fos.close ();
In.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
System.out.println ("Upload image to:/img/" + newfilename);
Save file address for JSP page Echo
String url = "/file/" +newfilename;
Model.addattribute ("file", url);
return "FileUpload";
}
}
Note In the configuration tomact, Service.xml is added in
<context path= "/file" docbase= "D:\Program files\tomcat7.0\webapps\ssm\img" debug= "0" reloadable= "true"/>
SSM implementation File Upload