This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Saving File from web upload

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i have an upload input file type and trying to save it to the server filesystem (windows).
This is what i have

Now Q1. Does the filepath have to include the name of the file you want to save?
Q2. How can i save it to the web server w/out knowing the driver letter. In other words, how can i make it use the relative path and not the absolute?

I'm getting this error with the current code
Error was java.io.FileNotFoundException: ..\images\venues\lounges\BiggerSizes.png (The system cannot find the path specified)
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Q1. Does the filepath have to include the name of the file you want to save?


Of course. How else would the file system know under which name to store the file?

Q2. How can i save it to the web server w/out knowing the driver letter. In other words, how can i make it use the relative path and not the absolute?


If the path is inside of the web app you can use the ServletContext.getRealPath method. It returns the absolute path for a given path relative to the web app root.
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is now this... and i get the same exact error as before.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what does baseLocation end up being? It can return null. Do you handle that case?
Does the path you've created exist?
Is it writable?
Does a file already exist in the destination directory?
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the baseLocation is empty.

I've also tried to get it from the jsp page and pass that as a session variable, but no good.


to answer the other questions, the past in theory (where i'm trying to save) does exist.

I believe it is writtable.. i'm testing it on my own pc using tomcat and i assume tomcat has the priveledges to write to its own directory.

Many files exist in the dir already.. onces that i have placed in there when running an initialization script to get test data into my DB.

How do i properly use the getRealPath code?
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using this code that seems to work

 
reply
    Bookmark Topic Watch Topic
  • New Topic