1 Configuring the virtual path method one: Modify the Tomcat configuration file
Configure the picture virtual directory on Tomcat and add it under Tomcat under conf/server.xml :
<docBase= "D:\develop\upload\temp" path= "/pic" reloadable= "false"/>
access http://localhost:8080/pic to access the pictures under D: \develop\upload\temp.
Method Two: Configure with Eclipse
Copy a picture to the upload below the D drive and then use the browser to access it.
URL: http://localhost:8080/pic/image name (including suffix name)
2 Adding a jar package
Implement the jar package that the image upload needs to add , such as:
Put two jar packages into the project's lib folder
3 Configuring the Upload Parser
Configuring the File Upload parser in springmvc.xml
<!--file Upload,iD must be set to Multipartresolver -<BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--set File upload size unit is b 5m=5*1024*1024 - < Propertyname= "Maxuploadsize"value= "5242880" /></Bean>
4 Preparing JSP pages
Here I prepare two JSP pages, 1 for file upload, and the other for uploading the echo of the image.
1 fileupload.jsp Page
<%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>FileUpload</title></Head><Body> <formAction= "${pagecontext.request.contextpath}/file/upload.action"Method= "POST" enctype= "Multipart/form-data" > <inputtype= "File"name= "FileName"> <inputtype= "Submit"value= "Upload"> </form></Body></HTML>
2 uploadsuccess.jsp Page
<%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body> <H1>The picture was uploaded successfully.</H1> <Divstyle= "width:600px;height:480px;border:2px solid green" > <imgalt= "Picture acquisition failed"src= "/pic/${picturename}"style= "width:100%;height:100%"> </Div></Body></HTML>
5 image Upload Filecontroller
PackageCom.test.springmvc.controller;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.UUID;ImportJavax.jws.WebParam.Mode;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;Importorg.springframework.web.multipart.MultipartFile; @Controller Public classFilecontroller {@RequestMapping (value= "/file/upload.action") Public voidFileUpload (HttpServletRequest request, HttpSession session, HttpServletResponse response, multipartfile filename) throwsexception{//image Upload//Set picture name, cannot repeat, can use UUIDString picname =uuid.randomuuid (). toString (). ReplaceAll ("-", "" "); //Get file nameString Oriname =Filename.getoriginalfilename (); //get picture suffixString extname = oriname.substring (Oriname.lastindexof ("."))); //Start uploadingFilename.transferto (NewFile ("e:/upload/" + Picname +extname)); //the name of the image after uploadingString Picturename = picname+Extname; //the picture name is placed in the fieldSession.setattribute ("Picturename", Picturename); //REDIRECT picture echo jsp pageResponse.sendredirect (Request.getcontextpath () + "/uploadsuccess.jsp"); }}
6 Testing
Springmvc Uploading Files