• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Upload through JSP

 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I don't know which forum to post this under. I already posted under the I/O forum. I'm trying to create a JSP that would let the user select a file from local system and upload it to a remote system and let the user download an uploaded file later. The remote server is an SFTP server.

My code was running fine (both upload and download) while i was testing on the local system (tomcat running on my system, and I'm selecting files from my system). But when the code was deployed on the actual testing servers, the code started failing with FileNotFoundException. I realised that the filename that I was sending to the code from the <input type="file"> field was not enough as my code would look for the file on the server. Same while downloading.

This is SFTP, so I can not use the regular FTP related code samples. I used the APIs from JSCH and code samples from Tim Archer's website.

Everything worked fine as long as the file was being uploaded from the server on which the code was running and/or the file was being downloaded to the server.

Can anyone please let me know if there is a way to use streams to send the data from the client filestream and make the code on the server use that stream instead of looking for the file on the server.

Any help is highly appreciated. This is very urgent for me!

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum. I have deleted the other copies of your post.

Servlets/JSPs only support file upload and download through HTTP. So you need to receive the file on the server through an HTTP file upload, using a library like Apache Commons FileUpload. Then you can move it to some other server using whatever protocol is appropriate. Same for downloading - you need to get the file from the other server to the server running the servlet container first; then you can stream it to the client using HTTP.

(As as aside, you really should not use JSPs for I/O operations -or any kind of processing-, use a servlet instead.)
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The topic title really demotivates me to help you.
Please read this: http://faq.javaranch.com/java/EaseUp
 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I edited the title. Sorry about that....
You need to be a little forgiving to the "Greenhorns"
But anyway, will definitely take care of this in future...


Coming back to the topic then, can you please direct me to some good code samples that specifically show the upload and downloading of the files to and from the server and specifically handling the location (as in where it will end up when uploading or downloading). I did look up on the web and there are many code samples for multipart/form-data type but most of them are not working for me.

Some code samples will really help me right now as the actual obstacle of JAVA - SFTP is not a problem for me anymore. I just need to get the file from the client to the server now and vice versa for download as you suggested.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This faq entry lists some 3rd party libraries for uploading to Java applications via HTTP.
http://faq.javaranch.com/java/FileUpload
Most people, these days, are using the Jakarta/commons one.

The online documentation has some simple code examples that make it easy to get going.

For downloading files, the SimpleStream demo app in our codebarn shows how to stream a file from within a web application's directory structure. If you are uploading to a directory outside of the web application's directory structure (best practice) then you would need to modify the SimpleStream app to read the file using java.io.FileReader.
http://faq.javaranch.com/java/CodeBarnSimpleStream
 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ben! That was helpful, especially the demo app...
Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!