A simple example of spring MVC (annotations) uploading a file, there are a few points to note
1.form enctype= "Multipart/form-data" This is required to upload files
2.applicationcontext.xml <bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "/> File upload configuration is not limited
Applicationcontext.xml
[HTML]View Plaincopyprint?
- <? 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:p= "http://www.springframework.org/ schema/p "
- xmlns:context="Http://www.springframework.org/schema/context"
- 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 "
- default-lazy-init="true">
- <!--start the annotation feature of spring MVC and complete the mapping of requests and annotations Pojo-
- <Bean class="Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false" />
- <!--also better to join defaultannotationhandlermapping, otherwise it will be overwritten by XML or other mappings! -
- <Bean class="Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <!--resolution of the Model view name, that is, the model view name is added before and after
- <Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix= "/web-inf/jsp/" p:suffix= ". jsp" />
- <!--support upload file value means the size of the uploaded file--
- <bean id= "multipartresolver" class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver ">
- <property name= "maxuploadsize" value= "10000000000000"/></bean>
- </Beans>
Uploadaction.java
[Java]View Plaincopyprint?
- Package com.codeif.action;
- Import Java.io.File;
- Import Java.util.Date;
- Import Javax.servlet.http.HttpServletRequest;
- Import Org.springframework.stereotype.Controller;
- Import Org.springframework.ui.ModelMap;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RequestParam;
- Import Org.springframework.web.multipart.MultipartFile;
- @Controller
- Public class Uploadaction {
- @RequestMapping (value = "/upload.do")
- Public String Upload (@RequestParam (value = "File", required = false) Multipartfile file, HttpServletRequest request, Modelmap model) {
- System.out.println ("start");
- String path = Request.getsession (). Getservletcontext (). Getrealpath ("upload");
- String fileName = File.getoriginalfilename ();
- String fileName = new Date (). GetTime () + ". jpg";
- SYSTEM.OUT.PRINTLN (path);
- File TargetFile = new File (path, fileName);
- if (!targetfile.exists ()) {
- Targetfile.mkdirs ();
- }
- //Save
- try {
- File.transferto (targetfile);
- In this way, the above method can be uploaded, but it is always reported that the output stream is occupied error.
- Savefilefrominputstream (
File.getinputstream (), Realpath + filePath, name + "." + FileType);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- Model.addattribute ("FileUrl", Request.getcontextpath () +"/upload/" +filename);
- return "result";
- }
- }
The implementation of Savefilefrominputstream () is as follows:
public void Savefilefrominputstream (InputStream stream, String path,
String filename) throws IOException {
{//path + "/" + filename
FileOutputStream fs = new FileOutputStream (path + "/" + filename);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((Byteread=stream.read (buffer))!=-1)
{
Bytesum+=byteread;
Fs.write (Buffer,0,byteread);
Fs.flush ();
}
Fs.close ();
Stream.Close ();
}
index.jsp
[HTML]View Plaincopyprint?
- <%@ page pageencoding="utf-8"%>
- <! DOCTYPE HTML>
- <html>
- <head>
- <Meta charset="Utf-8">
- <title> Upload image </title>
- </head>
- <body>
- <form action= "upload.do" method= "POST" enctype= "Multipart/form-data" >
- <input type="file" name="file" /> <input type="Submit" value= "Submit" /></form>
- </body>
- </html>
Spring MVC File Upload to folder (reprint + Experience)