PDA

View Full Version : Finding if files exist.. file on local machine, problems on server


uninvitedm
05-23-2007, 12:53 PM
I've been using Tomcat/local machine to produce some jsp pages for uploading files.

Files are selected and added to a listbox on a form, once the form is submitted - the filenames in the list are uploaded. Firstly there is a check to see if they exist. Something to the effect of..
File file = new File(filenames[x]);
file.exist(); for each file.

So typically in the listbox there will be C:\readme.txt or something to that effect. On local machine, this works perfectly - files are found and uploaded. However now, since I put this online to a server (which is an oracle server) when the form is submit -- the file can not be found. Initially when submit, the \'s would be removed.. (filenames which are not found are displayed in an error message) e.g C:\log.txt becomes C:log.txt so I added code to change this to unix separators.. C:\log.txt then becomes C:/log.txt ... still it cant find the file.

My experience with unix is fairly limited, is there a certain of writing a filename like that so that the jsp can pick it up? Or perhaps something I should look into to do with server itself? Any suggestions are appreciated. Thanks

ghell
05-23-2007, 06:09 PM
If you can change it from \ to /, could you change it from \ to \\? Would that preserve the \s? Or if you can change them from \ to /, could you not just change them back again when you need to check?

Unix does not use C: D: etc at all. All absolute paths will be something like /home/someone/log.txt (they are absolute because they start with the /. Usually if you want a relative path you either use nothing or . as in ./somedirectory/log.txt (using . is probably more reliable)

Are the files being actually written to disk? If so then surely it must have the correct path somewhere. I have not used JSP but it sounds like you are just uploading the file name, are you using multipart encoding on the form?

uninvitedm
05-24-2007, 07:38 AM
Hiya, thanks for the reply - i have uncovered that the problem is infact my code not so much the separators (as im new to this type of development).

I'm uploading the file by getting its inputstream and passing that to a method elsewhere (which uploads the file to a database as a BLOB object). The form i'm using currently uses a 'get' rather than 'post' and the action is run on the same page. I've read I'll need to upload the file to the server first but instead of then writing it, I'll pass to the inputsream BLOB method.

Does anyone have any sites or suggestions on how to upload the file like this? Thanks

ghell
05-24-2007, 01:44 PM
I would not have used get. The maximum length of get data I believe is around 8KiB. It may be better to use multipart encoding and post.

However, as I said, I have not tried JSP. I can do Java and I can do ASP, ASP.NET and php, but I don't know if the basic things like using multipart encoding to transmit files do not apply to JSP.

Uploading the file should be the same either way, just ratherthan using I/O to write it to the disk you pass it to your JDBC or whatever persistance options you are using. You must have the data properly uploaded in either case though and it is probably easier to test that is uploaded by writing it to the filesystem (make sure you have permissions of course) than to the database.

ess
05-24-2007, 05:22 PM
Why not use the static method "separator" of the File class so you don't have to worry about deploying your app on a Windows or Linux platform.

More info can be found here
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#separator

Cheers,
Ess