File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes how to handle file uploads in spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "how to handle file uploads in spring" Watch "how to handle file uploads in spring" New topic
Author

how to handle file uploads in spring

ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
Can somebody explain how to handle file uploads in spring framework.
Is it necessary to use multipartFile for the file upload.If not what are the other ways to do it.Expecting a prompt reply. Thanks
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Section 13.8 of the Spring Reference contains a file upload example.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
i am not interested in storing the file,Instead i need only the full-Path of the file to be stored.How to retrieve the full path.can post some example if available.
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
Mr.Nathan can you please suggest a way to solve my problem.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can't get the "full path" of the file being uploaded - just the file name. The file is embedded in the request.
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
Sir, What does the method getRealPath() actually do?Will i be able to use it in some way to fulfill my need.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Do you mean ServletContext.getRealPath()? No - this deals with mapping a URL to a file path on the server.
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
Sir,I have to upload the logo image.Can you explain where on the server should i transfer the file to.Suggest me the folder i have to upload it to.I was thinking resources folder in the WEB-INF is a better place.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Inside the WEB-INF isn't the best place for an image unless you need to protect regular access to them - regular images need to be somewhere net accessible, and WEB-INF is a "private" location for your web-app. You'd need a servlet to "front" your images if you stored them under WEB-INF (i.e. get the request, map the request to an image stored under WEB-INF, return the binary contents of the image from the servlet). If you store them in the web root of the web application, or in an accessible sub-folder, you can use them like regular images in your webapp.
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
Sir,Can you explain it in a more detailed fashion. I could not get the gist of it.Did you mean some other folder outside the webapps folder. What difference does it make if the images are outside the webapps folder inside (the resources folder inside the webapps). My dir structure is like this: tomcat->webapps->WEB-INF, resources->images->logos.jpg. Do i have to change this logos.jpg directly(i am replacing this file by another file with the same name) or do i have to link it to some other folder outside the webapps folder. Kindly explain. Thanks for the help Sir.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Normally the folder structure would be something like:
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
For web applications, construct a form with input type="file" in your jsp.
create your controller to extend abstract controller
then use something like this:
MultipartResolver resolver = new CommonsMultipartResolver();
MultipartHttpServletRequest request= resolver.resolveMultipart(httpRequest);
MultipartFile file=request.getFile("your file id from hte form");
[ December 23, 2008: Message edited by: Nick Potter ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to handle file uploads in spring
 
Similar Threads
How to handle exceptions in Spring
Top 10 Servlet/JSP misconceptions.
Uploading status display in spring
uploading file
Serving files outside document root