SpringMVC upload file

jsp

<form action="/upload" name="Form" id="Form" method="post" enctype="multipart/form-data">

 

xml

<!-- Upload interception, such as the maximum upload value and the minimum upload value 1024 is 1byte -->
	  <bean id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >   
		  <property name="maxUploadSize">    
	          <value>104857600</value>    
	       </property>   
	        <property name="maxInMemorySize">    
	            <value>4096</value>    
	        </property>   
	         <property name="defaultEncoding">    
	            <value>utf-8</value>    
	        </property>
    </bean>

The MultipartFile file  in the controller           must add @RequestParam    

@RequestMapping(value="/upload")
public ModerAndView save(
			@RequestParam(required=false) MultipartFile file
			) throws Exception{
//...

copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", "")

//...
 return mv
}

 -----Upload  File.separator path adaptive separator

 

/**
	 * Write the file to the upload directory of the current directory
	 *
	 * @param in
	 * @param fileName
	 * @throws IOException
	 */
	private static String copyFile(InputStream in, String dir, String realName)
			throws IOException {
		File file = new File(dir, realName);
		if (!file.exists()) {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileUtils.copyInputStreamToFile(in, file);
		return realName;
	}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326455459&siteId=291194637