This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I don't have how you have written code in your html or jsp. I think the following example solve your problem:
you need to put the commons-fileupload.jar in your classpath for the multipart resolver to work.
The following example shows how to use the CommonsMultipartResolver: <beans> <!-- lets use the Commons-based implementation of the MultipartResolver interface --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxUploadSize" value="100000"/> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /upload.form=fileUploadController </value> </property> </bean>
<bean id="fileUploadController" class="examples.FileUploadController"> <property name="commandClass" value="examples.FileUploadBean"/> <property name="formView" value="fileuploadform"/> <property name="successView" value="confirmation"/> </bean> </beans> To actually let the user upload a file, we have to create a (HTML) 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> </body> </html>
After that, create the controller and the actual class to hold the file property.
public class FileUploadController extends SimpleFormController { protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException { // cast the bean FileUploadBean bean = (FileUploadBean) command; let's see if there's content there byte[] file = bean.getFile(); if (file == null) { // the user did not upload anything } // well, let's do nothing with the bean for now and return return super.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 } } public class FileUploadBean { private byte[] file; public void setFile(byte[] file) { this.file = file; } public byte[] getFile() { return file; } }
phani babu
Greenhorn
Joined: May 16, 2006
Posts: 8
posted
0
Hi,
I don't have how you have written code in your html or jsp. I think the following example solve your problem:
you need to put the commons-fileupload.jar in your classpath for the multipart resolver to work.
The following example shows how to use the CommonsMultipartResolver: <beans> <!-- lets use the Commons-based implementation of the MultipartResolver interface --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxUploadSize" value="100000"/> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /upload.form=fileUploadController </value> </property> </bean>
<bean id="fileUploadController" class="examples.FileUploadController"> <property name="commandClass" value="examples.FileUploadBean"/> <property name="formView" value="fileuploadform"/> <property name="successView" value="confirmation"/> </bean> </beans> To actually let the user upload a file, we have to create a (HTML) 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> </body> </html>
After that, create the controller and the actual class to hold the file property.
public class FileUploadController extends SimpleFormController { protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException { // cast the bean FileUploadBean bean = (FileUploadBean) command; let's see if there's content there byte[] file = bean.getFile(); if (file == null) { // the user did not upload anything } // well, let's do nothing with the bean for now and return return super.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 } } public class FileUploadBean { private byte[] file; public void setFile(byte[] file) { this.file = file; } public byte[] getFile() { return file; } }
Ishika
Greenhorn
Joined: Aug 07, 2008
Posts: 12
posted
0
thnks,but my html file is same as the one you posted..but still it is not working.
phani babu
Greenhorn
Joined: May 16, 2006
Posts: 8
posted
0
Change the code in controller and bean definitions in configuration file as per above given example.It might solve your problem.
Please click on the My Profile link above and change your display name to meet the JavaRanch Naming Policy of using your real first and real last names.
Hello ranchers, Thanks a lot now i m able to upload file.But facing one problem. ie.If my file is text file ,it is not getting saved as text file,it is getting saved without any extension.For opening that file i have always do a openwith -->Notepad. any help will be appreciated. Thanks in advance.
Please follow the request to correct your display name before your next post. This is not optional; accounts with invalid display names are generally closed quickly.
As to your question, what do you mean by "downloading"? Assuming this is a web app, can't you place the file in a public directory, and provide a link to it? If not, you can stream the file contents to the client as shown in http://faq.javaranch.com/java/CodeBarnSimpleStream