Friday, December 28, 2012

Spring MVC file upload example


Below is a running example to upload a file using spring , please add the below lone in your application-context file to add CommonsMultipartResolver


   <bean id="multipartResolver"

        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

Write your JSP page like below , Please do add HTML code if you want to run it as a seprate page

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div  xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:page="urn:jsptagdir:/WEB-INF/tags/form" xmlns:table="urn:jsptagdir:/WEB-INF/tags/form/fields" version="2.0">

         <form id="fileuploadForm" action="fileupload" method="POST" enctype="multipart/form-data" class="cleanform">
          <c:if test="${successfull==Y}">
              <c:out value="File Was Successfully uploaded"></c:out>
          </c:if>
          <label for="file">File</label>
          <input id="file" type="file" name="file" />
          <p><button type="submit">Upload</button></p> 
         </form>
</div>

Write the controller to handle requests related to file upload
package com.aramco.peasd.dbp.web;

import java.io.IOException;
//import org.springframework.mvc.extensions.ajax.AjaxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/fileupload")
public class FileUploadController {
       @ModelAttribute
       public void ajaxAttribute(WebRequest request, Model model) {

//            model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));

       }
       @RequestMapping(method=RequestMethod.GET)
       public void fileUploadForm() {
  

       }
@RequestMapping(method=RequestMethod.POST,produces =
"text/html"
)
       public String processUpload(@RequestParam MultipartFile file, Model model) throws IOException {
        

              model.addAttribute(  "message", "File '" + file.getOriginalFilename() + "' uploaded successfully");
              model.addAttribute("successfull", "Y");
              //**This byte array can then be sent to any content managment server  to save the file
              byte[] fileByteArray=file.getBytes();
//**Or you may want to save the file using java IO on a folder somewhere in you server             
              return "fileupload/showFileUpload";
//this will render to a page called showFileUpload.jspx
       }
       @RequestMapping(params="fileupload")
       public String showFileUploadPage(){
              return "fileupload/showFileUpload";
       }
}

 

1 comment:

  1. If you don't mention in your request mapping
    produces = "text/html"

    Some browsers will kill you with the below alerts

    Launching application
    Varifying application requirements.This may take a few moments.

    and then the below Alert

    Cannot Start Applicaiton

    Cannont continue. the application is improperly formatted.Contact the application vendor for assistance.

    This happens because the browser is trying to read your response not as text/heml and i't thinking that it has to open an application to load this response however you are just returning a string or forwarding to another URL.

    The solution is to always mention produces = "text/html" in your request mapping

    ReplyDelete