1

Tutorials for File Uploading. I copied the code and saved it my project. I change the

String filePath = context.getInitParameter("file-upload");

into

String filePath = context.getInitParameter("uploads");

Because I want to save the image in the folder named uploads. This folder is located outside the folder where I have put the script. I even tried

String filePath = context.getInitParameter("../uploads");

But in the output code

out.println("Uploaded Filename: " + filePath + fileName + "<br>");

FilePath is null and there is no image in the folder. How to properly set the filePath?

Hash
  • 4,647
  • 5
  • 21
  • 39
Brownman Revival
  • 3,620
  • 9
  • 31
  • 69
  • 2
    For your safety, please ignore so-called tutorial sites when they are full of advertisement banners (tutorialspoint, javabeat, roseindia, etc). They are full of low quality code and bad practices. The correct and simplest way to upload a file is answered here: http://stackoverflow.com/q/2422468 – BalusC Aug 13 '16 at 07:30
  • @BalusC ive seen your answers already not just the link many others. but i need to make it work with out servlet is it possible or not possible?i am just using form submit i can also use ajax i just want to store the image then later on retrieve it for update purpose. – Brownman Revival Aug 13 '16 at 07:40
  • Java code in JSP file is not different from a normal Java servlet class. I.e absolutely no changes are required to get exactly the same Java code to run in a JSP file as compared to a normal Java servlet class (because, after all, JSP files are converted to normal servlet classes). It's only a bad practice for reasons mentioned in http://stackoverflow.com/q/3177733. You only end up with unreusable, unmaintainable, undebuggable, etc non-OO code which smells too much like PHP. – BalusC Aug 13 '16 at 08:50

3 Answers3

0

String filePath = context.getInitParameter("file-upload")

you are getting the value from web.xml, If you want to store in dirrefernt location change the file path in web.xml as follows

<param-value>
    c:\apache-tomcat-5.5.29\webapps\data\uploads
</param-value> 
ravthiru
  • 8,878
  • 2
  • 43
  • 52
0

The init parameter comes from the web.xml. There you will have to declare for that servlet an init parameter named uploads with a value of your destination. Please study some doc's about servlets or webapps.

Sorry for this short answer but it seems there is some basic knowledge about servlets missing here so digging into some more tutorials and doc's makes more sense than trying here to explain some fundamentals. ;-)

Aviator
  • 512
  • 3
  • 7
0

You can store directory details in properties file as well--

in catalina.properties store values like below(this file is located at $CATALINA_HOME/conf/catalina.properties)

my.home=directory/path

(WEB-INF/web.xml)

<context-param>
<param-name>uploads</param-name>
<param-value>${my.home}</param-value>
</context-param>

and then try

String filePath = context.getInitParameter("uploads");
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36