springmvc file upload tomcat virtual path settings

1. Loading rack package

  commons-fileupload.jar

  commons-io-2.4.jar

2. In some projects with a small amount of file storage, the uploaded files are generally placed in the directory of the project itself, but when the amount of uploaded files increases, the folder capacity of the project itself will also be It is getting bigger and bigger, which is not conducive to the packaging and deployment of the project, and the startup and operation of the project will also be affected, so here, the local drive letter is used to store the file, and the virtual path is set to access the file, using tomcat 7 

   Double-click Tomcat in the MyEclipse tool and set the virtual path in the web modules page

   

   When accessing the /pic path in the URL, you can access the local F:\java\upload file path

3. Configure the file parser in the springmvc.xml configuration file

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>

4. Write a file upload Controller class

/**
* Parameters: uploaded file, save path, save file name
* @throws IOException
* @throws IllegalStateException
* */
@RequestMapping("uploadImg.action")
public String upload(Model model,@RequestParam MultipartFile file) throws IllegalStateException , IOException{

String savePath = "F:\\java\\upload";//upload folder path
String fileName = file.getOriginalFilename();//Get file name
String date = null;//System date
String newFileName = null ;//The stored file name
createFold = new CreateFold();//Get the current system date and create the save folder on the day
String path = null;//The final saved file path

if(file!=null&&fileName!=null&&fileName. length()>0){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.format(new Date());//Get the system time and convert it to a string
path = createFold.createFoldFile(savePath, date);//Create a folder of the day in the specified path folder to save the uploaded file
newFileName = UUID.randomUUID()+fileName.substring(fileName.lastIndexOf(".")) ;
File newFile = new File(path+newFileName);
file.transferTo(newFile);//Save the file to the specified path
}
model.addAttribute("img", date+"/"+newFileName);

return "jsp/ImgUploadTest ";
}

 CreateFold class:

/**
* String path: under which path to create the folder
* String date: current system date string
* Return value: newly created folder path
* **/
public String createFoldFile(String path,String date){
String newPath = path+"\\"+date;
File fold = new File(newPath);
if(!fold.exists()){
fold.mkdir();
}
return newPath+"\\";
}

5. Write the front page ImgUploadTest.jsp

<form action="uploadImg.action" method="post" enctype="multipart/form-data">
<c:if test="${img!=null }">
<img alt="" src="/pic/${img }" width="100" height="100">
</c:if>
<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>

 

Guess you like

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