jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes file upload in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "file upload in servlet" Watch "file upload in servlet" New topic
Author

file upload in servlet

vijayakumar durai
Ranch Hand

Joined: Aug 18, 2008
Posts: 153
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
Marshal

Joined: Mar 22, 2005
Posts: 41129
    
  45
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?


Ping & DNS - my free Android networking tools app
vijayakumar durai
Ranch Hand

Joined: Aug 18, 2008
Posts: 153
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
Marshal

Joined: Mar 22, 2005
Posts: 41129
    
  45
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

Joined: Oct 03, 2008
Posts: 1
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

Joined: Aug 18, 2008
Posts: 153
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
Marshal

Joined: Mar 22, 2005
Posts: 41129
    
  45
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

Joined: Aug 18, 2008
Posts: 153
yes i want to check the file is already exist in the directory
vijayakumar durai
Ranch Hand

Joined: Aug 18, 2008
Posts: 153
can any body tell how to check file is already in the directory?
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
http://java.sun.com/javase/6/docs/api/java/io/File.html#exists()


My Blog
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: file upload in servlet
 
Similar Threads
using file chooser
How can I upload a file(*.doc,*.zip...) within a servlet?
Unable to upload file using Commons HTTPClient and Commons FileUpload
How to upload a file using a servlet?
How to upload a file along with form data to a database using servlet