Spring MVC notes (vii) SPRINGMVC file upload

Source: Internet
Author: User

Create SPRINGMVC Project Springmvcfileupload, import SPRINGMVC related jar and Commons-io.jar, Commons-fileupload.jar;

Create SPRINGMVC default profile springmvcfileupload-servlet.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd ">    <Context:component-scanBase-package= "Com.bwy" />    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/jsp/" />        < Propertyname= "suffix"value= ". jsp" />    </Bean>    <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"/></Beans>

To create a file model Filemodel.java:

 Package Com.bwy.fileupload; Import Org.springframework.web.multipart.MultipartFile;  Public class Filemodel {  private  multipartfile file;    Public multipartfile GetFile () {    return  file;  }    Public void setfile (multipartfile file) {    this. File = file;  }} 

To write an upload page fileupload.jsp:

<%@ Page Language="Java"pageencoding="UTF-8"%><%@ taglib Prefix="form"URI="Http://www.springframework.org/tags/form"%><!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=iso-8859-1"><title>Spring MVC File Upload</title></Head><Body>    <Form:formAction= "/springmvcfileupload/upload.do"Method= "POST"Modelattribute= "FileUpload"enctype= "Multipart/form-data">Please select a file to upload:<inputtype= "File"name= "File" />        <inputtype= "Submit"value= "Submit Upload" />    </Form:form></Body></HTML>

Upload Success Page success.jsp:

<%@ Page Language="Java"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=iso-8859-1"><title>Insert Title here</title></Head><Body>${filename}, upload success! </Body></HTML>

Write the upload file control class: Fileuploadcontroller.java:

 PackageCom.bwy.fileupload;ImportJava.io.File;Importjava.io.IOException;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.ModelMap;Importorg.springframework.util.FileCopyUtils;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.servlet.ModelAndView;/*** File Upload Control class * *@authorAdministrator **/@Controller Public classFileuploadcontroller {@RequestMapping (value= "/uploadpage", method =requestmethod.get) PublicModelandview fileuploadpage () {return NewModelandview ("FileUpload", "command",NewFilemodel ()); } @RequestMapping (Value= "/upload.do", method =requestmethod.post) PublicString Upload (filemodel file, Modelmap model, HttpServletRequest request) {//define File upload directoryString Fileuploadpath =request.getsession (). Getservletcontext (). Getrealpath ("") + File.separator + "upload" +File.separator; //Build DirectoryFile UploadFile =NewFile (Fileuploadpath); if(!uploadfile.exists ())    {Uploadfile.mkdir (); }    Try {      //Copy the file, assuming that the file name after upload is: xx/xx/1.jpgFilecopyutils.copy (File.getfile (). GetBytes (),NewFile (Fileuploadpath + "1.jpg"))); } Catch(IOException e) {e.printstacktrace (); } model.addattribute ("FileName", File.getfile (). Getoriginalfilename ()); return"Success"; }}

Start the Tomcat Run project:

Spring MVC notes (vii) SPRINGMVC file upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.