• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to handle file uploads in spring

 
ramanuja varun
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Section 13.8 of the Spring Reference contains a file upload example.
 
ramanuja varun
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr.Nathan can you please suggest a way to solve my problem.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean ServletContext.getRealPath()? No - this deals with mapping a URL to a file path on the server.
 
ramanuja varun
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally the folder structure would be something like:
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic