1, file upload
a, introducing dependent
b, spring-mvc.xml profile
C, the configuration page
URL: "$ {} pageContext.request.contextPath / User / testResponseBody",
Data: {},
type: "POST",
dataType : "JSON",
Success: function (Data) {
Alert (Data [0] .username);
Alert (Data [. 1] .username);
}
});
d. After the introduction of static resource static resources must be released
Introducing fileUpload automatically dependent commons-io
<% -
form file upload premise
1) must be presented in a way POST
2) must form a type: multipart / form-data, form data multifunction
3) must have a file of the form element type =
- %>
/**
- Declare variable parameters to receive data
-
- /
@RequestMapping ( "/ Upload")
public String Upload (String username, a MultipartFile Upload, the HttpServletRequest Request) {
// System.out.println (username);
.. 1 target path //
// Get the running path project
String realPath = . Request.getSession () GetServletContext () the getRealPath ( "/ Upload");.
// determines whether the path exists
File realfile = new new File (realpath);
iF {(realFile.exists ()!)
realFile.mkdirs ();
} /
. / 2 to obtain a unique file name (including the extension)
String uuidName = UUID.randomUUID () toString () the replace ( "-", "");..
// get the extension: get the file name
// get the real file name
string OriginalFilename = upload.getOriginalFilename ();
// string interception, access to the file extension
OriginalFilename.substring extendName = String (originalFilename.lastIndexOf () ".");
System.out.println (extendName);
// unique file name
String fileName = uuidName + extendName;
System.out.println (fileName);
// file upload
// transferTo: performing file upload
@ parameter file: file catalog
the try {
upload.transferTo (new new file (realfile, fileName));
} the catch (IOException E) {
e.printStackTrace ();
} R & lt
eturn "Show" ;
}
- /