• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to handle file uploads in spring

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr.Nathan can you please suggest a way to solve my problem.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally the folder structure would be something like:
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Are you here to take over the surface world? Because this tiny ad will stop you!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic