Regarding the method of uploading pictures in springmvc, the editor has compiled two methods for you, the specific content is as follows:
The first type: (placed at the corresponding location of the physical address under the project)
a. Path writing method:
String basePath="/WEB-INF/resources/upload";
String filePathName= request.getSession().getServletContext().getRealPath(basePath);
b. Actual path:
D:/WorkSpace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/XYT/WEB-INF/resources/upload/Image name
c. Access path: http://localhost:8080/XYT/resources/upload/image name
d. Prerequisite: As long as this project can run.
The second type: (create a virtual path, configure server.xml under Tomcat, create storage path and access path)
1. Path writing:
String filePathName=Constant.IMG_PATH+File.separator+"upload";
Where: public static final String IMG_PATH = "E://Java//img";
2. Path configuration:
Server.xml configuration
<Host name="localhost" appBase="webapps"unpackWARs="true" autoDeploy="true"><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log." suffix=".txt"pattern="%h %l %u %t "%r" %s %b" /><!-- add(save pictures) --><Context path="/upload" docBase="E:/Java/img/upload"></Context></Host>
3. Actual path: E:/Java/img/upload
4. Access path: http://localhost:8080/upload/image name
5. Reference: http://my.oschina.net/pingdy/blog/381001
6. Prerequisite: The Tomcat server must be opened
Example: Example of uploading pictures: (multiple pictures can be uploaded)
JSONObject rs=new JSONObject();CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());String url="";if (multipartResolver.isMultipart(request)) {MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;Iterator<String> iter = multiRequest.getFileNames(); while (iter.hasNext()) {MultipartFile file = multiRequest.getFile((String) iter.next());if (file != null) {String originalFileName = file.getOriginalFilename();String[] f = originalFileName.split("//.");String ext = "";if(f!=null && f.length>1){ext = f[f.length-1];System.out.println(ext);}System.out.println(allowImgType==null);if(!allowImgType.contains(ext.toUpperCase())){rs.put("code", "ERR_UPLOAD_0003");rs.put("msg", "type error");return rs;}//String basePath="/WEB-INF/resources/upload";//String filePathName = request.getSession().getServletContext().getRealPath(basePath);String filePathName=Constant.IMG_PATH+File.separator+"upload";url = filePathName;System.out.println(url);//Record in the path after uploading. File localFile = new File(filePathName); if(!localFile.exists()){ localFile.mkdir(); } //compressString fname = new Date().getTime()+"."+ext;String originalFname = fname.substring(0,fname.indexOf("."))+"_original."+ext;String fileName = filePathName + File.separator + fname;String oFileName = filePathName + File.separator + originalFname;File infile = new File(fileName);File oFile = new File(oFileName); try{ImageHelper.compress(file.getInputStream(), 600, infile);file.transferTo(oFile);//original Upload the original image JSONObject obj = new JSONObject();rs.put("code", Constant.CODE_SUCCESS);rs.put("data", obj.toString());}catch(Exception e){rs.put("code", "ERR_UPLOAD_0001");rs.put("msg", "ERR_UPLOAD_0001");e.printStackTrace();return rs;}}The above is related to uploading pictures and accessing SpringMVC, and I hope it will be helpful to everyone.