Jquery + springMVC implements file upload and jqueryspringmvc
This article is based on
1. Set up the Jquery + SpringMVC + Spring + Hibernate + MySQL Platform
2. How to obtain common web engineering paths
3. Common file (folder) Handling Methods tool class
I. Preparations
1. Click here to download the relevant files and place the files in the corresponding folder of the ims project.
II. Introduction to jar packages
1. commons-fileupload-1.3.1.jar
3. Related program code
1. applicationContext. xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="maxUploadSize"> <value>52428800</value> </property> <property name="maxInMemorySize"> <value>2048</value> </property> </bean> </beans>
View Code
2. TestController. java
Package com. ims. web. controller; import java. io. IOException; import java. util. hashMap; import java. util. map; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. multipart. multipartFile; import org. springframework. web. multipart. multipartHttpServletRequest; import org. springframework. web. servlet. modelAndView; import com. alibaba. fastjson. JSON; import com. ims. common. fileUtil; @ Controller @ RequestMapping ("test") public class TestController extends BaseController {@ RequestMapping ("view") public ModelAndView test () {ModelAndView view = new ModelAndView ("test. jsp "); return view ;}@ RequestMapping (" fileUpload ") public void fileUpload (MultipartHttpServletRequest multipartRequest) {Map <String, Object> result = new HashMap <String, object> (); try {MultipartFile uploadFile = multipartRequest. getFile ("uploadFile"); String fileName = uploadFile. getOriginalFilename (); String uploadPath = System. getProperty ("webapp. root ") +" uploadFile \ "; FileUtil. saveFileFromInputStream (uploadFile. getInputStream (), uploadPath + fileName);} catch (IOException e) {result. put ("status", STATUS_ERROR); result. put ("message", "File Upload Failed");} ajax (JSON. toJSONString (result), "text/html ");}}View Code
3. test. jsp
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"> <% @ page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> View Code
Iv. Test
Access: http: // localhost: 8090/ims/test/view. do
Select a file and Click Upload. the uploaded file is displayed under the % project release directory % \ WebContent \ uploadFile.