Announcement Announcement Module
Collapse
No announcement yet.
Problem with file upload in spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with file upload in spring

    Hi

    I am trying to create a program where I can upload the file. I am referring to the http://static.springsource.org/sprin...l/ch16s08.html

    First JSP
    Code:
    <body>
    	<form method="post" action="upload.do" enctype="multipart/form-data">
    		<input type="file" id="upload" ><br>
    		<input type="submit" id="submit" value="Upload Image" />
    	</form>
    </body>
    Command Class
    Code:
    import org.springframework.web.multipart.MultipartFile;
    
    public class User {
    
        private MultipartFile img;
    
        public MultipartFile getImg() {
    		return img;
    	}
    
    	public void setImg(MultipartFile img) {
    		this.img = img;
    	}
    
    }
    Configuration file
    Code:
    	<bean name="/upload.do" class="com.bansal.controller.MyController">
    		<property name="commandName" value="user" />
    		<property name="commandClass" value="com.bansal.command.User" />
    		<property name="formView" value="DisplayHomePage" />
    		<property name="successView" value="DisplayUserInfo" />
    	</bean>
    
    	<bean id="viewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix">
    			<value>/WEB-INF/view/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
    
    	<bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
    Controller Class
    Code:
    package com.bansal.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.validation.BindException;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    import com.bansal.command.User;
    
    public class MyController extends SimpleFormController {
    
    	protected ModelAndView onSubmit(HttpServletRequest request,
    			HttpServletResponse response, Object command, BindException errors)
    			throws Exception {
    		User user = (User) command;
    		response.setContentType("image/jpeg");
    
    		MultipartFile file = user.getImg();
    		String name = "none";
    		if (file != null) {
    			
    			name = file.getOriginalFilename();
    		}
    		return new ModelAndView("DisplayImage", "image", name);
    	}
    
    }
    Output JSP
    Code:
    <body>
    <h3>Your Image</h3>
    ${image} name 
    </body>
    I am getting "Your image none name". Means Controller is got getting the file.

    Could someone please point me where I am wrong.

  • #2
    And why should it work. There is nothing in your form object that matches the actual form. The parameter is named file and not img as in your form object. So basically your uploaded file goes nowhere.

    Comment


    • #3
      Thanks Marten for pointing me out

      It is working now. I have more problem. If in the controller I write
      Code:
      response.setContentType("image/jpeg");
      response.getOutputStream().write(user.getFile().getBytes());
      then the image is displayed onn the output JSP. But when I tried to use the Bytes[] and MultipartFile object as a Model and directly use the model object output JSP it print hashcode of Object. Is there is any way to display the image on JSP.

      Code:
      response.setContentType("image/jpeg");
      MultipartFile file = user.getFile();
      return new ModelAndView("DisplayImage", "image", file.getBytes());
      ON JSP
      Code:
      <body>
      <h3>Your Image</h3>
      ${image};
      </body>

      Comment


      • #4
        I convert the MultipartFile to the String using StringMultipartFileEditor. Now My User Object contain the User Name and the Image as a String. After that I send the User Object to the JSP.

        On the JSP I am able to print the user name but it will print the Image as a String not the Image
        Code:
        <td>First Name: ${user.fName}</td>
        <td>Image Name <%
        				
        				out.write(((User) request.getAttribute("user")).getImage());
        			%>
        			</td>

        Help me out in this please

        Comment

        Working...
        X