aspose file tools*
The moose likes Struts and the fly likes Commons FileUpload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Commons FileUpload" Watch "Commons FileUpload" New topic
Author

Commons FileUpload

Lavanya Mothilal
Ranch Hand

Joined: Dec 27, 2007
Posts: 30
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.


Thanks,<br />Lavanya
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Dec 27, 2007
Posts: 30
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

Joined: Jul 17, 2007
Posts: 21
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.


Regards,<br />tanveer<br /> <br />SCJP 1.4 (86%)<br />SCWCD 1.5 (98%)
Lavanya Mothilal
Ranch Hand

Joined: Dec 27, 2007
Posts: 30
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

Joined: Jul 17, 2007
Posts: 21
Ok i got it.. I think following code should work. This will redirect to failure in case file does not exist.

/
tanveer chowdry
Greenhorn

Joined: Jul 17, 2007
Posts: 21
Hey Lavanya,
The code i sent previously won't work. Please try with the following code. It should work.


Lavanya Mothilal
Ranch Hand

Joined: Dec 27, 2007
Posts: 30
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Commons FileUpload
 
Similar Threads
File Upload Filetypes
Need a File Upload API
Cannot get the complete filename
Commons File Upload Tutorial
HTTPS File Uploader