• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

file upload in servlet

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am using multipart request for uploading the file in servlet if the user upload the same name for different file it will be overwrited can any body tell how to solve the problem?



thanks in advance
 
Rancher
Posts: 43045
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file will only be overwritten if you save it under the same name. You should check if the file exists before saving the newly uploaded file,a dn take appropriate action if that's the case. Which file upload library are you using?
 
vijayakumar durai
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not able to check whether the file is already exist or not.multipart request will upload the file in folder at the time of request is coming.so image also overwrited
 
Ulf Dittmer
Rancher
Posts: 43045
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of software is handling the request? Why can't it check for existing files? That seems like a rather fundamental thing for it to be able to do.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple way to solve it is to generate a random number and add it to your file name before the upload.
 
vijayakumar durai
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MultipartRequest request = new MultipartRequest(req,context.getRealPath((context.getInitParameter("ProductImagePath"))),5*1024*1024);
Enumeration<String> e = request.getFileNames();
ArrayList<String> imageFileNames = new ArrayList<String>();
while(e.hasMoreElements()) {
imageFileNames.add(request.getFilesystemName(e.nextElement().toString()));

}

hai ulf
can you tell how to check file is already exist using multipartrequest?

thanks
 
Ulf Dittmer
Rancher
Posts: 43045
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar with the API you're using, but isn't "request.getFilesystemName(e.nextElement().toString())" a file name? I'm assuming that you know in which directory files should be stored, so you should be able to check whether a file of this name already exists in that directory, no?
 
vijayakumar durai
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i want to check the file is already exist in the directory
 
vijayakumar durai
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body tell how to check file is already in the directory?
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/javase/6/docs/api/java/io/File.html#exists()
reply
    Bookmark Topic Watch Topic
  • New Topic