• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving File from web upload  RSS feed

 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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)
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
My code is now this... and i get the same exact error as before.
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
using this code that seems to work

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!