0

Hello i am making a servlet that gets the image from a

Everything in my servlet works fine. The only problem is that i want to know what is the name of the uploaded image so that i can store its full path in a database. How to i so that?

This is the code that upload the file but, it doesn't provide me the actual name of the original image. f.getName gives me the name of my tag.

Part f= request.getPart("imgCoverInserisci");
InputStream imageInputStream = f.getInputStream();
System.out.println("Path where image will be saved: "+request.getContextPath()+"/Immagini/");
   /*returns null*/        String     nomeFile=request.getParameter("imgCoverInserisci");
   f.getName(); //return name of input tag
  FileOutputStream out = new FileOutputStream ("C:\\Users\\Salvatore\\Documents\\NetBeansProjects\\TestFumettopoli\\web\\Immagini\\copertineFumetti\\"+nomeFile);
// write bytes taken from uploaded file to target file
int ch = imageInputStream.read();
while (ch != -1) {
                  out.write(ch);
                  ch = imageInputStream.read();
}
out.close();
imageInputStream.close();
  • use Apache commons, it has a module for file uploading in servlet, and also returns the client file name –  Jul 16 '13 at 09:30
  • is there any way to do it in servlet 3.0 ? – Salvatore Servodio Jul 16 '13 at 09:38
  • yes dude it works with servlet 3 too :) –  Jul 16 '13 at 09:41
  • check [here](http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm) and [here](http://commons.apache.org/proper/commons-fileupload/using.html) :D –  Jul 16 '13 at 09:43

1 Answers1

0

I solved this problem in a different way. I used javascript to parse the name of the file image everytime the Onchange event occured. The javascript function then assigned the name of the file in a hidden input tag. Later the servlet only needed to read this as a parameter and the trick is done!

the javascript code is here

Community
  • 1
  • 1