--- --- restore content begins
First, the traditional way
1. Import jar package file upload
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
2. Write file upload jsp page
<h3>文件上传</h3>
<form action="user/fileupload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传文件"/>
</form>
3. Write file upload Controller Controller (file upload traditional way)
@Controller @ RequestMapping ( "/ the User" ) public class the UserController { / ** * File upload * @return * / @ RequestMapping ( "/ fileupload1" ) public String fileupload1 (the HttpServletRequest Request) throws Exception { System.out.println ( " file upload " ); // use fileupload complete file upload component // location uploaded String path = Request.getSession () GetServletContext () the getRealPath (.." / uploads / " ); // determines whether the path exists file file = newFile (path); IF (! File.Exists ()) { // create the folder file.mkdirs (); } // parse the request object, upload files to obtain entry DiskFileItemFactory = Factory's new new DiskFileItemFactory (); ServletFileUpload the Upload = new new ServletFileUpload (Factory); // parse Request List <the FileItem> items = upload.parseRequest (Request); // iterate for (the FileItem item: items) { // for determining, whether the current object is an item to upload a file entry iF (item. isFormField ()) { //Description is a common form to } the else { // instructions for uploading a file entry // Get the name of the uploaded file String filename = item.getName (); // the name of the file set unique value, UUID String UUID = UUID.randomUUID () . .toString () the replace ( "-", "" ); filename = uuid + filename; // complete file upload item.write ( new new file (path, filename)); // delete temporary files item.delete (); } } return "Success" ; } }
Two, springmvc way
1, the configuration file parser in springmvc.xml
<! - Configuration file parser ->
<bean the above mentioned id = "the MultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver">
<! - Configuration file upload size ->
<Property name = " maxUploadSize "value =" 10485760 "/>
</ the bean>
2, springmvc upload files
/ ** * SpringMVC upload * @param Request * @return * @throws Exception * / @ RequestMapping ( "/ fileupload2" ) public String fileupload2 (the HttpServletRequest Request, MultipartFile the Upload) throws Exception { System.out.println ( "File SpringMVC Upload " ); // use fileupload complete file upload component // upload location String path = Request.getSession () GetServletContext () the getRealPath (.." / uploads / " ); // determines whether the path exists file file = new newFile (path); IF (! File.Exists ()) { // create the folder file.mkdirs (); } // instructions to upload a file entry // Get the name of the uploaded file String filename = upload.getOriginalFilename (); // the name of the file to set a unique value, uuid String uuid = UUID.randomUUID () toString () the replace ( "-", ".". ); filename = uuid + "_" + filename; // complete file upload upload .transferTo ( new new File (path, filename)); return "Success" ; }
Three, springmvc upload files across servers
1, set up image server
2, need to import jar package
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.18.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18.1</version> </dependency>
--- end --- restore content