My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads

Spring MVC Multipart Upload support for GAE

Version History:

  • 0.3 - download
    1. supports Spring 3.x (>= Spring 3.0 RC1)
    2. compiled in Java6
    3. supports maven2
  • 0.2-java5 - download
    1. supports Spring 3.x (>= Spring 3.0 RC1)
    2. Compiled in Java5
    3. Thanks to pmatiello for the ant build file
  • 0.2 - download
    1. Supports Spring 3.x (>= Spring 3.0 RC1)
    2. Compiled in Java6
  • 0.1 - download
    1. supports Spring 2.x (< Spring 3.0 RC1)
    2. Compiled in Java6

Overview:

This implementation removes all the file handling related codes from the following files to make it work in Google App Engine.

  1. org.springframework.web.multipart.commons
  2. org.apache.commons.fileupload.disk
  3. org.apache.commons.io.output

Maven Settings:

<repositories>
    <repository>
        <id>gmultipart</id>
        <url>http://gmultipart.googlecode.com/svn/repo/m2</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>gmultipart</groupId>
        <artifactId>gmultipart</artifactId>
        <version>0.3</version>
    </dependency>
</dependencies>

Demo:

  1. Edit your dispatcher-servlet.xml and add bean org.gmr.web.multipart.GMultipartResolver as shown below.Note: All the property settings that can be done in org.springframework.web.multipart.commons.CommonsMultipartResolver, can also be done in org.gmr.web.multipart.GMultipartResolver, except setting maxInMemorySize property.
  2. <!--
    In this implementation, maxInMemorySize is defaulted to maxUploadSize property value.
    -->
    <bean id="multipartResolver" class="org.gmr.web.multipart.GMultipartResolver">
        <property name="maxUploadSize" value="1048576" />
    </bean>
  3. Lets consider this simple multipart html form.
  4. <html>
      <body>
        <form action="/save" method="post" enctype="multipart/form-data">
          <input name="comment">
          <input name="file1" type="file">
          <input name="file2" type="file">
          <input name="file3" type="file">
          <input name="file4" type="file">
          <input name="file5" type="file">
        </form>
      </body>
    </html>
  5. Create a CommentForm.java to represent HTML form input fields 'comment', 'file1', 'file2', 'file3', 'file4', 'file5'.
  6. public class CommentForm {
    	private String comment;
    	private MultipartFile file1;
    	private MultipartFile file2;
    	private MultipartFile file3;
    	private MultipartFile file4;
    	private MultipartFile file5;
    
    	public String getComment() {
    		return comment;
    	}
    
    	public void setComment(String comment) {
    		this.comment = comment;
    	}
    
    	public void setFile1(MultipartFile file1) {
    		this.file1 = file1;
    	}
    
    	public void setFile2(MultipartFile file2) {
    		this.file2 = file2;
    	}
    
    	public void setFile3(MultipartFile file3) {
    		this.file3 = file3;
    	}
    
    	public void setFile4(MultipartFile file4) {
    		this.file4 = file4;
    	}
    
    	public void setFile5(MultipartFile file5) {
    		this.file5 = file5;
    	}
    
    	public MultipartFile[] getFiles() {
    		List<MultipartFile> files = new ArrayList<MultipartFile>();
    		if (file1 != null) {
    			files.add(file1);
    		}
    		if (file2 != null) {
    			files.add(file2);
    		}
    		if (file3 != null) {
    			files.add(file3);
    		}
    		if (file4 != null) {
    			files.add(file4);
    		}
    		if (file5 != null) {
    			files.add(file5);
    		}
    		return files.toArray(new MultipartFile[files.size()]);
    	}
    }
  7. Create a CommentController.java to handle form post.
  8. @Controller
    public class CommentController {
    	@RequestMapping(value = "/save", method = RequestMethod.POST)
    	public String addComment(CommentForm commentForm) throws IOException {
    
    		MultipartFile[] files = commentForm.getFiles();
    
    		for (int i = 0; i < files.length; i++) {
    			// Do stuffs!!!
    		}
    
    		return "redirect:/saved"; // redirect after processing!!!
    	}
    }
Powered by Google Project Hosting