The traditional file upload
The traditional way of uploading files, refer to the files and applications visit our upload exist on the same server. And the upload is complete, your browser may jump.
1. SpringMVC framework provides MultipartFile object that represents a file upload request variable names must be the attribute name of the form file name is the same label.
<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.4version> </dependency>
/ * File * SpringMVC way of uploading * * @param Request * @return * @throws Exception * / @ RequestMapping (value = "/ fileupload2" ) public String fileupload2 (the HttpServletRequest Request, MultipartFile the Upload) throws Exception { System.out. println ( "file upload SpringMVC way ..." ); // first get to the directory you want to upload String path = request.getSession () getServletContext () getRealPath ( "/ uploads.". ); // create file object one will upload files to the path under file file = new new file (path); //Analyzing path exists, if not, creating the path IF (! File.Exists ()) { file.mkdirs (); } // Get the name of the file to upload String filename = upload.getOriginalFilename (); String UUID = the UUID . .randomUUID () toString () replaceAll ( "-", "." ) .toUpperCase (); // the name of the file unification filename = uuid + "_" + filename; // upload files upload.transferTo ( new new file (File, filename)); return "Success" ; }
<! - upload the configuration file parser -> < the bean ID = "the MultipartResolver" <! - ID is a fixed value -> class = "org.springframework.web.multipart.commons.CommonsMultipartResolver"> <! - upload files up to 5MB -> < Property name = "maxUploadSize" > < value > 5242880 </ value > </ Property > </ bean >
Cross-way server file upload:
Setting up an image server, a simple test, then you can run a different Tomcat server ports on the same computer
2. Additional import the required cross-server 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>
3. Write a JSP file upload page
4. Write Controller
/ ** * SpringMVC way of cross-server file upload * * @param Request * @return * @throws Exception * / @ RequestMapping (value = "/ FileUpload" ) public String FileUpload (MultipartFile the Upload) throws Exception { System.out.println ( "cross-server approach SpringMVC upload files ..." ); // request path defined picture image server server uploads folder must exist, otherwise an error String path = "http: // localhost : 9090 / image / uploads / " ; // get the name of the uploaded file String filename = upload.getOriginalFilename (); String uuidUUID.randomUUID = () toString () replaceAll ( "-", ".". ) .ToUpperCase (); // the name of the file unification filename = uuid + "_" + filename; // upload files to the image server / / create a client-side objects client client = Client.create (); // connection image server the WebResource the WebResource = client.resource (path + filename); // upload files webResource.put (upload.getBytes ()); return "Success" ; }
5. upload the configuration file parser