This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes Unable to upload file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Unable to upload file" Watch "Unable to upload file" New topic
Author

Unable to upload file

Ishika
Greenhorn

Joined: Aug 07, 2008
Posts: 12
hello ranchers,


My Controller Code :



My problem is :In my controller
MultipartFile multipart = multipartRequest.getFile("file1");
returns null....
Which is it returning null....

Please help me....
phani babu
Greenhorn

Joined: May 16, 2006
Posts: 8
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;
}
}
phani babu
Greenhorn

Joined: May 16, 2006
Posts: 8
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
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
Change the code in controller and bean definitions in configuration file as per above given example.It might solve your problem.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

"Ishika"

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.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ishika
Greenhorn

Joined: Aug 07, 2008
Posts: 12
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.
anuj patel
Greenhorn

Joined: Jan 02, 2008
Posts: 10
Hi,

Please mention that how are you writing the filecontent to FileOutputStream

Thanks & Regards,
Anuj Patel
Ishika
Greenhorn

Joined: Aug 07, 2008
Posts: 12

hey,
got that working to..
just added the following code which gets the file name.



Any ways thnks for the help.
Ishika
Greenhorn

Joined: Aug 07, 2008
Posts: 12
Hello ranchers,
can any one provide me with the code for downloading a file using spring.
thnks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
Hello "Ishika"-

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
Ishika
Greenhorn

Joined: Aug 07, 2008
Posts: 12
hello All,

I m able to download a file from server using spring..I can download text file,doc files etc. But not images.
I m using this code...




Any help will be appreciated..
thnks in advance
 
 
subject: Unable to upload file