Three ways to upload files in SpringMVC, just upload the code, you can tell at a glance
front desk:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><form name="serForm" action="/SpringMVC006/fileUpload" method="post" enctype="multipart/form-data"><h1>Upload files using streaming</h1><input type="file" name="file"><input type="submit" value="upload"//form><form name="Form2" action="/SpringMVC006/fileUpload2" method="post" enctype="multipart/form-data"><h1>Upload files using file.transfer method provided by multipart</h1><input type="file" name="file"><input type="submit" value="upload"/></form><form name="Form2" action="/SpringMVC006/springUpload" method="post" enctype="multipart/form-data"><h1>Upload the file using the method provided by spring mvc</h1><input type="file" name="file"><input type="submit" value="upload"/></form></body></html>
Configuration:
<!-- Multipart file upload--><bean id="multipartResolver"> <property name="maxUploadSize" value="104857600" /> <property name="maxInMemorySize" value="4096" /> <property name="defaultEncoding" value="UTF-8"></property></bean>
Backstage:
Method 1:
/* * Upload the file through stream* @RequestParam("file") Encapsulate the file obtained by the name=file control into CommonsMultipartFile object*/ @RequestMapping("fileUpload") public String fileUpload(@RequestParam("file") CommonsMultipartFile file) throws IOException { //Used to detect program run time long startTime=System.currentTimeMillis(); System.out.println("fileName:"+file.getOriginalFilename()); try { //Get the output stream OutputStream os=new FileOutputStream("E:/"+new Date().getTime()+file.getOriginalFilename()); //Get the input stream CommonsMultipartFile to directly obtain the file's stream InputStream is=file.getInputStream(); int temp; //Read and write bytes one by one to while((temp=is.read())!=(-1)) { os.write(temp); } os.flush(); os.close(); is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } long endTime=System.currentTimeMillis(); System.out.println("Run time of method one: "+String.valueOf(endTime-startTime)+"ms"); return "/success"; }Method 2:
/* * Use file.Transto to save the uploaded file*/ @RequestMapping("fileUpload2") public String fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException { long startTime=System.currentTimeMillis(); System.out.println("fileName:"+file.getOriginalFilename()); String path="E:/"+new Date().getTime()+file.getOriginalFilename(); File newFile=new File(path); //Write the file directly through the CommonsMultipartFile method (note this time) file.transferTo(newFile); long endTime=System.currentTimeMillis(); System.out.println("The running time of method two: "+String.valueOf(endTime-startTime)+"ms"); return "/success"; }Method 3:
/* *Use the method of uploading files provided by spring*/ @RequestMapping("springUpload") public String springUpload(HttpServletRequest request) throws IllegalStateException, IOException { long startTime=System.currentTimeMillis(); //Initialize the current context to CommonsMutipartResolver (multipart parser) CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver( request.getSession().getServletContext()); //Check whether there is enctype="multipart/form-data" in form if(multipartResolver.isMultipart(request)) { //Change request into multipart request MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request; //Get all file names in multiRequest Iterator iter=multiRequest.getFileNames(); while(iter.hasNext()) { //Transfuse all files at once MultipartFile file=multiRequest.getFile(iter.next().toString()); if(file!=null) { String path="E:/springUpload"+file.getOriginalFilename(); //Upload file.transferTo(new File(path)); } } } long endTime=System.currentTimeMillis(); System.out.println("The running time of method three: "+String.valueOf(endTime-startTime)+"ms"); return "/success"; }Let's take a look at the test upload time:
The first time I used a 4M file:
fileName: test.rar
Method 1 run time: 14712ms
fileName: test.rar
Method 2 run time: 5ms
Method 3 run time: 4ms
The second time: I use a 50M file
The progress of the method is very slow, and it will take 5 minutes.
Method 2 run time: 67ms
Method 3 run time: 80ms
From the test results, we can see that the method of uploading files using springMVC is much faster!
For the result of test 2: It may be that the method three must be searched one by one, so it should be slower. But generally speaking, we have Method 3 because it can provide us with more methods
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.