• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file upload in servlet

 
vijayakumar durai
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
federico gentilucci
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
can any body tell how to check file is already in the directory?
 
Vinod K Singh
Ranch Hand
Posts: 198
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic