upload a local file from local (client) machine to server machine

I have a problem for upload a file from my client machine to server machine. In my webapplication I use the following implamentation using jsp:

page: upload.jsp

<!--tag form without enctype="multipart/form-data"-->

<form id="myform" name="myform" method="post" action="uploadprocess.jsp">

Attach: <input type="file" name="attach"/>

<input type="submit" name="BtnSend" value="Send" />

</form>

...

page: uploadprocess.jsp

File anexo = new File(attach);

FileInputStream fis = new FileInputStream(attach);

[..] //here I have more codes that are ok...

...

the problema is:

when I'm using my webapplication in server machine (where java application server is running), it works perfect when the code line "FileInputStream fis = new FileInputStream(attach);" is read, because the application can recognize the path passed in var "attach". But, where I'm using in another client machine, where the webapplication isn't running, this recognize the file system of the server machine and not the client (local) machine....

Could anybody help me to solve this problem?!

[1187 byte] By [afssdna] at [2007-11-27 3:44:38]
# 1

As you have found out, the java.io classes are not useful to you, as they naturally access that servers file system (that is where java is running after all)

What you need to do is

1 - make your form enctype to be multipart/formdata. The form HAS to submit the file in this manner.

It will then upload the file to the server when you push the submit button.

The server code needs to read it from the input stream.

Best way to do that is via a library - try the jakarta file upload component: http://jakarta.apache.org/commons/fileupload/using.html

evnafetsa at 2007-7-12 8:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

hi evnafets. You were right! java.io classes didn't solved my problem. So, as you said, I used jakarta apache classes with multipart/form-data form and it solved my problem! thank you very much!!

And befor finish, let me ask you: do you know how I can make the download now?! Is there a specific class for download like upload?! In my webapplication, the file is stored in database and I can download it in server machine from the own server machine or client machine, but I can't downlod the file in client machine. How can I do that?!

If you don't know about that, don't worry. You solved my main troble: the upload hehehe. Thank you very much!

[ ]'s

afssdn

afssdna at 2007-7-12 8:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Download is basically streaming the file contents to the HttpServletResponse#getOutputStream(). Don't forget to set the content headers accordingly. You can find useful downloadFile() snippets at bottom of the article at http://balusc.xs4all.nl/srv/dev-jep-pdf.html
BalusCa at 2007-7-12 8:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Great!!! It磗 a very good article! I'll study its and implement my webapplication with theses codes! hehe Thank you very much![]'safssdn
afssdna at 2007-7-12 8:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...