Thursday 28 February 2013

File Upload & Image Conversion In Spring 3.0 Using Java.ImageIO

web.xml


  HelloWorldExampleWithSpring3MVCInEclipse
  
    Spring MVC Dispatcher Servlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      
    /WEB-INF/app-config.xml
   
    
    1
  
  
    Spring MVC Dispatcher Servlet
    /
  
  
  
   index.jsp
  




app-config.xml



 
 
 
 
 
 
 
 
  
  
 
 
 
    
        
        
    




UploadFormController.java
package com;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.domain.UploadForm;

@Controller
public class UploadFormController implements HandlerExceptionResolver{
 
 @RequestMapping(value="/FileUploadForm", method=RequestMethod.GET)
 public String showForm(ModelMap model){
  UploadForm form = new UploadForm();
  model.addAttribute("FORM", form);
  return "FileUploadForm";
 }

 @RequestMapping(value="/fileAction", method=RequestMethod.POST)
 public String processForm(@ModelAttribute(value="FORM") UploadForm form,BindingResult result){
  if(!result.hasErrors()){
   FileOutputStream outputStream = null;
   String filePath = System.getProperty("java.io.tmpdir") + "/" + form.getFile().getOriginalFilename();
   try {
    outputStream = new FileOutputStream(new File(filePath));
    outputStream.write(form.getFile().getFileItem().get()); 
    outputStream.close();
   } catch (Exception e) {
    System.out.println("Error while saving file");
    return "FileUploadForm";
   }
   return "success";
  }else{
   return "FileUploadForm";
  }  
 }
 

 @Override
 public ModelAndView resolveException(HttpServletRequest arg0,
   HttpServletResponse arg1, Object arg2, Exception exception) {
  Map model = new HashMap();
        if (exception instanceof MaxUploadSizeExceededException)
        {
            model.put("errors", "File size should be less then "+((MaxUploadSizeExceededException)exception).getMaxUploadSize()+" byte.");
        } else
        {
            model.put("errors", "Unexpected error: " + exception.getMessage());
        }
        model.put("FORM", new UploadForm());
        return new ModelAndView("/FileUploadForm", (Map) model);

 }
 
 
 @RequestMapping(value = "/image.htm", method = RequestMethod.POST)
 public ModelAndView getFile(@ModelAttribute(value="FORM") UploadForm form) throws IOException {
     try {
      System.out.println("In Image Processing Action");
      String fileType = form.getFile().getContentType();
      
         // Retrieve image from the classpath.
         InputStream is = form.getFile().getInputStream(); 

         // Prepare buffered image.
         BufferedImage img = ImageIO.read(is);

         // Create a byte array output stream.
         ByteArrayOutputStream bao = new ByteArrayOutputStream();
         
         File output = new File("F://output.tiff");
         
         // Write to output stream
         ImageIO.write(img, "jpg", output);
         
         return new ModelAndView("success");
     } catch (IOException e) 
     {
         throw new RuntimeException(e);
     }
 }

}

UploadForm.java
package com.domain;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class UploadForm {
 
 private String name = null;
 private CommonsMultipartFile file = null;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public CommonsMultipartFile getFile() {
  return file;
 }
 public void setFile(CommonsMultipartFile file) {
  this.file = file;
  this.name = file.getOriginalFilename();
 }
 
 
}


FileUploadForm.jsp

<%@ page session="true" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

 
  File Upload with Spring 3 MVC
  
 
 
  

File Upload Form


${errors}
Name :

success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




File Upload Confirmation Page


 

File has been uploaded successfully.


File Name : ${FORM.name}.



0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...