tutorial for file upload in spring

tutorial for file upload in spring

View Answers

December 19, 2008 at 12:22 AM

Hi friend,

Code to help in solving the problem :

FileUploadController.java file

package example;


import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.net.BindException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import service.PriceIncrease;

import example.FileUploadBean;

public class FileUploadController extends SimpleFormController
{

protected final Log logger = LogFactory.getLog(getClass());

protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws ServletException, IOException , Exception {

// cast the bean
FileUploadBean bean = (FileUploadBean) command;

// let's see if there's content there
byte[] file = bean.getFile();
if (file == null) {
// hmm, that's strange, the user did not upload anything
}

// well, let's do nothing with the bean for now and return
return onSubmit(request, response, command, errors);
}

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
throws ServletException {
// to actually be able to convert Multipart instance to byte[]
// we have to register a custom editor
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}

}



package example;

import java.io.Serializable;

/*public class FileUploadBean implements Serializable {

private static final long serialVersionUID = 1L;

private byte[] file;

public FileUploadBean() {
super();
}

public FileUploadBean(byte[] file) {
super();
setFile(file);
}

public void setFile(byte[] file) {
this.file = file;
}

public byte[] getFile() {
return file;
}

}*/



My FileUploadBean.java file

import org.springframework.web.multipart.MultipartFile;

public class FileUploadBean {

private byte[] file;

public void setFile(byte[] file) {
this.file = file;
}

public byte[] getFile() {
return file;
}
}

December 19, 2008 at 12:23 AM




fileuploadform.jsp file

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"; %>
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="upload.form" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>

<!-- <spring:bind path="fileUpload.file">
<input type="file" name="file" id="file" class="file medium" value="<c:out value="${status.value}"/>"/>
</spring:bind>
-->
</body>
</html>


the problem is when confire this using in springapp-servelt.xml like below i am getting the requested resource not found 404 error (when i start the tomcat5.50

the bean configuration is :

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">

<value>
/upload.form=fileUploadController
</value>
</property>
</bean>

<bean id="fileUploadController" class="example.FileUploadController">
<property name="commandClass" value="example.FileUploadBean"/>
<property name="formView" value="fileuploadform"/>
<property name="successView" value="confirmation"/>
</bean>

Thanks

December 19, 2008 at 12:34 AM

thanks for the example dude. But this example uses Simple Form Controller and beans . I want to use a Controller which implements just the Controller interface









Related Tutorials/Questions & Answers:
tutorial for file upload in spring - Spring
tutorial for file upload in spring  Is there tutorial available for uploading file using spring framework. The example in the spring reference uses...; fileuploadform.jsp file Upload a file please Please upload a file
file upload in spring - Spring
file upload in spring  Is it necessary to use Multipart support in spring framework for file uploads.If not please let me know the other ways to do
Advertisements
file upload using spring - Spring
file upload using spring  How to upload a file in server using spring framework
Spring 2.5 MVC File Upload
Spring 2.5 MVC File Upload       Spring 2.5 MVC File Upload This tutorial explains how to upload... easily create file upload and form based application in Spring MVC
File Upload Tutorial With Examples In JSP
File Upload Tutorial With Examples In JSP  ... by Using JSP This tutorial will help you to understand how you can upload a file... with image This tutorial will help you to understand how you can upload
file upload in spring 2.5 - Spring
file upload in spring 2.5  hi, i facing problem in file upload in spring 2.5 my FileUploadController.java file package example... file; } } my fileuploadform.jsp file Upload
Spring 3 MVC File Upload, Spring MVC File Upload
upload example using Spring 3.0 MVC module. In this tutorial we are creating file...Spring 3 MVC File Upload Creating file upload example using Spring 3 MVC Learn how to create file upload application using Spring MVC module of the Spring
Multipart support for file upload in Spring MVC
In this section, you will learn about multipart support for file upload in Spring MVC
File Upload Tutorial With Examples In JSP
File Upload Tutorial With Examples In JSP  ... by Using JSP This tutorial will help you to understand how you can upload a file... with image This tutorial will help you to understand how you can upload
How to create d db for upload files using bolb i m followin dat upload file in db tutorial
followin dat upload file in db tutorial   Create a table named 'file...How to create d db for upload files using bolb i m followin dat upload file...;<b>Choose the file To Upload:</b></td> <td><INPUT
File Upload
File Upload  when i execute the image upload to mysql database it shows an exception that file path cannot be found
How to create d db for upload files using bolb i m followin dat upload file in db tutorial
How to create d db for upload files using bolb i m followin dat upload file... wht u have posted, still der is some problem,My program creates class file, But whn i run d application it only uploades the file in C:/uploadedfiles, and i get
File Upload
File Upload  Hi I need script / string in PHP where I uploaded the file in html frontend and it should sent the uploaded file as attachment to my email address
file upload
file upload  how to recieve the uploaded file on the next jsp page for modification if its uploaded to the previous page using a from
File Upload in Struts.
File Upload in Struts.  How to do File Upload in Struts
Spring 3.2 MVC, Upload File in a specific folder
In this Spring 3.2 MVC tutorial, you will learn about uploading file in a specified folder
upload and save the file
upload and save the file  upload and save a file from a computer to specific location of remote computer using jsp
File Upload Servlet 3.0 Example
File Upload Servlet 3.0 Example In this tutorial you will learn how to upload... annotation to upload the file. Earlier versions than the servlet 3.0 specification were not involves in handling the file upload. By introducing the @MultipartConfig
jsp upload file to server
jsp upload file to server  How to create and upload file to server in JSP?   Find the given example that explains how to upload single and multiple file on server using JSP
upload a file - Framework
upload a file  Hi to All, I need to upload a file from the client and that upload file should be sent to our co., mail please if any one provide.... Thankyou in Advance.  Hi friend, For upload a file in JSP visit
Upload file to MS Access
Upload file to MS Access  Iam beginner and need to upload file to msaccess using jsp . Iam using netbeans platform. Kindly explain do I need to mention Attachment format in the table
servlet file upload
servlet file upload  please tell me the complete code to upload a file on localhost using servlet
Struts File Upload and Save
Struts File Upload and Save  ... regarding  "Struts file upload example". It does not contain any... example will  provide you with the code to upload the file ,in the upload
Struts upload file - Framework
Struts upload file  Hi, I have upload a file from struts... and send to file upload struts..how to get the sheets and data in that sheets Thanks.  Hi friend, For upload a file in struts visit to : http
Upload file with ather Fieldn
Upload file with ather Fieldn  hello i want upload file with other record to database for my project,like that (FileID, Filename,Filedescrption,Filedate,file_upload) see( upload file with path) i can send data to database
AJAX file upload
AJAX file upload If you want to upload file asynchronously means without... Description You need to create a form with file fields you wish to upload... and browse button for seeking file in local directory to upload file using
File Upload and Retrive files
File Upload and Retrive files  Can any body help me am getting an error in uploading file. into mysql database.... thank's in advance
Multiple File Upload in PHP
Multiple File Upload in PHP  Hi, I am beginner in PHP scripting language. I am very interested to learn PHP application. So, can anyone explain or provide related reference about how to Multiple file upload in PHP. Thanks
file upload plugin in jquery
file upload plugin in jquery  file upload plugin in jquery   You can use the following code to use the JQuery plugin. $(document).ready(function() { $("#uploadbutton").jsupload({ action: "addFile.do
File upload in JSP
File upload in JSP  hi! In my previous interview i got two questions which i could not answer regarding file upload and annotations. I want to know which is the best method can be used for file upload. Whether moving them
Java upload file to ftp
Java upload file to ftp  Hi, How to uploaded file to a ftp server in Java? Thanks   Hi, It's very easy to write a program in Java.... View complete example at FTP File Upload in Java Thanks
File Upload - JSP-Servlet
File Upload  Hi everyone, I am facing file uploading problem.the multiple file upload code of roseindia is working on localhost but the same code is not working on server.i think the package commons-fileupload-1.2.jar
Multiple file upload - Struts
Multiple file upload  HI all, I m trying to upload multiple files... need to upload is dynamically generated. I have tried using formfile. and also... in solving the problem : Upload page
Ajax file upload
Ajax file upload  I am developing a application for image upload using ajax and servlet. The image should be converted in byte[] and must be saved in oracle databse as blob. After uploading the image it should be displayed
Image upload file - JSP-Servlet
://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml http://www.roseindia.net/jsp/file_upload/Sinle_upload.xhtml.shtml Thanks...Image upload file  I want a code for image upload jsp or servlet
zip file upload in php - WebSevices
zip file upload in php  This is not a normal upload. we know the code for normal upload. i need the zip file upload code in php. how to upload zipfile using php? and how to unzip using php? please i dont
File upload - JSP-Servlet
File upload  I am trying to do a file upload program. But, it shows... ----------------------------------------------------------------------------- Display file upload form to the user <... the file To Upload
file upload using JSP
file upload using JSP  I have created a form to upload a file...="java" %> <HTML> <HEAD><TITLE>Display file upload form... To Upload:</b></td> <td><INPUT NAME="file" TYPE="file"><
File upload - JSP-Servlet
to do a file upload part. For this, i designed two files one for input and other...(); %>You have successfully upload the file by the name... in the problem part]   Hi friend, Display file upload form to the user
To Upload and insert the CSV file into Database
To Upload and insert the CSV file into Database... to upload a CSV file through JSP and insert it into the database. For this, we have...;HTml> <HEAD><TITLE>Display file upload form to the user<
file upload download - Java Beginners
file upload download  how to upload and download files from one system to another using java.io.* and java.net.* only please send me code
Struts File Upload Example - Struts
Struts File Upload Example  hi, when i tried the struts file.../struts/strutsfileupload.shtml i have succeeded. but when i try to upload file with size>1 MB , getting error "file size is large". if i want to upload
java file upload in struts - Struts
java file upload in struts  i need code for upload and download file using struts in flex.plese help me  Hi Friend, Please visit the following links: http://www.roseindia.net/struts/strutsfileupload.shtml http
file upload error - JSP-Servlet
file upload error  Hello friends, In my project when i am trying to upload a file i used the roseindia.net coding from URL: http://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml
upload the pdf file only - Struts
upload the pdf file only  plz tell me,in my web application i want to upload only pdf file.but i m getting the problem when anyone change the extesion of EXE or TEXT with .pdf its also upload.plz give me response ASAP plz
Drag and Drop file upload in DOJO
Drag and Drop file upload in DOJO  Hi, We are working with DOJO for creating the UI for the web application.Our requirement is "Drag and Drop files from desktop to the browser for upload". Is this feature supported in DOJO? Any
How to upload file on FTP Server
How to upload file on FTP Server  In my application there is a requirement of uploading the file on FTP server. How to upload file on FTP Server... is good and can be used in Java applications. Read FTP File Upload in Java for more
Struts 2 File Upload error
Struts 2 File Upload error  Hi! I am trying implement a file upload using Struts 2, I use this article, but now the server response the error... link: File Upload In Struts2ADS_TO_REPLACE_2 Thanks
Java FTP file upload example
Java FTP file upload example  Where I can find Java FTP file upload example? What is the link of the FTP examples on your website? Thanks   Hi, We have many examples of Java FTP file upload. We are using Apache
Upload file without brouse button
Upload file without brouse button  Hi i want to upload 7 files... the files (.jpeg) .But I am not able to enter that address into file field can any body help me .I am using FormFile API to upload files.I dont want to change my

Ads