Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Commons FileUpload

 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using Commons File Upload to upload a file. The problem is, the file gets uploaded even if it doesn't actually exist i.e a new empty file is uploaded (I am using a shared path for storing the uploaded files). My application has to thrown an error to the user if the file to be uploaded doesn't exist in the local system. Please let me know if you have any idea on this.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lavanya Mothilal wrote:Hi,

I am using Commons File Upload to upload a file.


if you are using struts,then no need to go for Commons File Upload,instead use FormFile to upload. google it for more detail
 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Yes Now I use the FormFile to upload. Even in this FormFile, I am not able to check the existence of the file. Have given my sample code below.
FileUploadForm fileUploadForm = (FileUploadForm) form;
FormFile file = fileUploadForm.getFile();
/**Here I would like to include the check to verify the existence of the file **/

String filePath = "Network path to upload the file";
if(!fileName.equals("")){
File fileToCreate = new File(filePath, fileName);
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
fileOutStream.write(file.getFileData());
fileOutStream.flush();
fileOutStream.close();
}
 
tanveer chowdry
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt understand why are putting a file exist check. If you're using fileupload, you'll get a browse button. i.e. it lets you select the file you want.

We this, why there is need to check, because you'll have to select a existing file.
 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tanveer Chowdry,
You are right. The user can use the "Browse" button to choose the existing file. But there is a provision for the user to directly type in the location of the file in the text box. The user can type location (even non existent file location). So I need to include a check for whether the file exists.
 
tanveer chowdry
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i got it.. I think following code should work. This will redirect to failure in case file does not exist.

/
 
tanveer chowdry
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Lavanya,
The code i sent previously won't work. Please try with the following code. It should work.


 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tanveer. But this code doesn't work.

String fileName = myFile.getFileName();
File fileToCreate = new File(fileName);
if (fileToCreate.exists()) {


- This if condition always return false even if the file actually exists. I think this is because the fileName resolves only to the file's name and not its actual path. Any other idea?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic