Springmvc implementing file Uploads

Source: Internet
Author: User

1 Importing related jar packages via commons-fileupload

Commons-fileupload,commons-io

2 Configuring the SPRINGMVC configuration resolver

Mvc:

class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >            <property name= " Defaultencoding "value=" Utf-8 "></property>            <property name=" maxuploadsize "value=" 10485760000 "> </property>            <property name= "maxinmemorysize" value= "40960" ></property>     </bean>

3 JSP page

<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >File:<input type= "File" name= "file"/> <input type= "Submit" value= "Upload"/> </form> </body>

4 controller Code

@Controller Public classFileuploadcontroller {@RequestMapping ("/upload")     PublicString FileUpload (@RequestParam ("file") Commonsmultipartfile file,httpservletrequest req)throwsioexception{//Get file name//File.getoriginalfilename (); //get the path to the uploaded fileString Path = Req.getrealpath ("/fileupload"); InputStream is=File.getinputstream (); OutputStream OS=NewFileOutputStream (NewFile (Path,file.getoriginalfilename ())); intLen = 0; byte[] buffer =New byte[400];  while(Len=is.read (buffer))!=-1) {os.write (buffer,0, Len);            Os.close ();        Is.close (); }                    return"/index.jsp"; }}

Bulk upload of code

@RequestMapping ("/batch")     PublicString FileUpload (@RequestParam ("File") Commonsmultipartfile file[], httpservletrequest req)throwsioexception{//Get file name//File.getoriginalfilename (); //get the path to the uploaded fileString Path = Req.getrealpath ("/fileupload");  for(inti = 0; i < file.length; i++) {InputStream is=File[i].getinputstream (); OutputStream OS=NewFileOutputStream (NewFile (Path,file[i].getoriginalfilename ())); intLen = 0; byte[] buffer =New byte[400];  while(Len=is.read (buffer))!=-1) os.write (buffer,0, Len);            Os.close ();                Is.close (); }                    return"/index.jsp"; }

Springmvc implementing file Uploads

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.