• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

FTP Upload

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This an urgent problem..
I am trying to use a java frame to upload files from the client side to the server side... can this be possible or not?
and what methods should I use ?
another question is , can a java applet do this uploading process?
Thanks in advance,
Jimi.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There can be many ways to do it.
Basically there is a client (Frame/applet) running on HOST-A which needs to upload to HOST-B.
The client on HOST-A cannot directly access the file-system of the HOST-B. What we need is a server running on HOST-B which can receive data from a client and write to the local file system. HOST-A will then establish a socket connection with the server on HOST-B and pass data through the socket to the server and the server will write the data to the local file system.
The server on HOST-B can be based on internet protocols or it can be your own custom protocol.
Your subject mentions FTP- so apparently you want to use FTP protocol. For this you need to start an FTP server on HOST-B. Then use the URL & URLConnection classes in your client (application/applet) to establish connection with the ftp server. You can use the setRequestProperty() to set the method to 'store' because that is the method used for file uploading. Then you need to get an OutputStream from the URLConnection, and then write obtain the contents of your file (through RAF/FileInputStream etc) and write the contents through the Output Stream. The ftp server on HOST-B will do the rest.
Instead of using an FTP server you may use a web-server running on HOST-B to call a servlet. You can make a servlet, which would obtain data from doPost() method and write the data to the local file system. Such a servlet can also be invoked by using URL/URLConnection classes on your client running on HOST-A.
Instead of using ftp/web-server, you may design your own lightweight server running on your specified port on HOST-B. The server will set up a ServerSocket() to listen to requests from the remote client. The remote client (HOST-A) will then open a socket connection using Socket class, then get an output stream and write the file contents to the output stream. The server on HOST-B will receive the contents through the input stream obtained from its socket and then write to the local file system of HOST-B.
Instead of using Socket, you may use RMI Server running on HOST-B. Expose a method in the RemoteInterface such as write(String, file), which will be implemented by the RMI Server. The client on HOST-A will then call the write() method to upload the contents of the file to HOST-B.
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Rahol for your spesified options ,
but what do you advise me to use as I don't know which option is better with java Applet.
I would be grateful for you (or any one) if u can lead me to a starting code that may help me doing my job.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what the requirements of your project are. Do you want to upload the file to the server from which you downloaded the applet or to some other host? Is there a proxy or firewall intervening? What kind of server h/w & s/w are you running? Is this a toy project or is it a live-project?
Maybe the easiest option is to program a servlet and deploy it on the Web-server. In a FileWriterServlet extends HttpServlet define the doPost(HttpServletRequest req, HttpServletResponse res) as follows:-
{
try {
String file=req.getParameter("file");
String data=req.getParameter("fileData");
FileWriter fw=new FileWriter(file);
fw.write(data.getChars());
} catch (IOException ex)
}
Inside the applet define some method: writeFile(String sourceFile, String destFile, String url) : where the sourcefile is the full file name of the file on the local system, destFile is the full file name of the file that you want to write on the server's file system and the url is a String pointing to the Servlet. Then implement with this method with the following code:-

{
try {
RandomAccessFile raf=new RandomAccessFile(sourceFile, "rw");
byte[] b=new byte[raf.length()];
raf.read(b);
String data=new String(b);
URL u=new URL(url);
URLConnection con=u.openConnection();
con.setDoOutput(true);
con.connect();
PrintWriter out=new PrintWriter(connection.getOutputStream());
out.print(file + "=" + URLEncoder.encode(destFile) + '&');
out.print(fileData + "=" + URLEncoder.encode(data) + '\n');
out.close();
} catch (IOException ex) { }
}
Try it and let me know if it works.
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You may like to know about my project.
Actually, I want to upload video/image files from a client machine Host-A to a database server Host-B , and the Applet used should came from another machine Host-C which is a web server . I want you to know that there is a proxy intervening .
I am running windows 2000( NT built-in) ,the server program is IIS or NT HTTP server (or any one if you have a suggestion).The processor is Pentuim3.
It is -of course- a live project.
My questions are:
-Does your suggestion (servlet) cosidered to appropriate with my case?
-Is it really the easiest way?
-what about the program that is available in the following link:
www.javaranch.com/ubb/Forum38/HTML/000066.html
is it helpful? and which option does it use?
Thanks in advance.

 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code I gave in the previous post will not work, since you don't want to write on the originating home server and secondly it will handle text-data (though this can be rectified).
You will need a server running on HOST-B to do your writing. Unfortunately the SecurityManager will prevent the applet on HOST-A from directly connecting to HOST-B since that is not its originating 'home' server. It is difficult to overcome this limitation if you are using applet, although I think it can be done using signed applets and security permissions. But surely it is not worth that hassle. However an indirect connection- from HOST-A to HOST-C and from HOST-C TO HOST-B may be possible.
The code www.javaranch.com/ubb/Forum38/HTML/000066.html is for an ftp client- If you run this on HOST-A, you will then need to install and run a FTP server on the HOST-B. As you can read, the ftp-client will make the Socket connection from HOST-A TO ftp-server on HOST-B- BUT because of security restrictions on the applet you will NOT be able to connect and hence you will not be able to ftp.
Of course this limitation would not be there if you were using an application instead of applet. You can then run an ftp-server on HOST-B and use the relevant part of the ftp-client code for uploading the file from HOST-A TO HOST-B. You can also use the URL and URLConnection classes to upload through ftp, instead of using the above code.
But do you want to use application or applets only? And do you want to run an ftp-server? Do you want to code your own ftp-server also? I think that will be overkill. And why do you wish to use the ftp protocol at all?- Why not use your own simple protocol? Code your own lightweight server. All you need to do is open a socket connection to the server and write through the socket connection (subject to security permissions/proxy-settings etc).
For an applet, one solution is that you have one server running on HOST-C and another server running on HOST-B. The applet on HOST-A can connect to its originating home server i.e. HOST-C and HOST-C can then connect to the database on HOST-B. Applet on HOST-A will write to the server on HOST-C and in turn the server on HOST-C will write to server on HOST-B i.e. HOST-C will act as a sort of proxy-server for HOST-B. Coding the servers is very simple in Java and it can be done in a few lines of code.
Waiting to hear your comments. We can experiment with different options, after you make your requirements clearer.

[This message has been edited by Rahul Rathore (edited March 28, 2001).]
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,
Thank you very much for your reply.
Some important things that I want you to know is that I want the Uploading process to "work" using the three specified hosts A B and C , and host-A (The client host) is running an Applet. and please don't forget that the files are image/video ones.
Any other requirements are not important for me and I want you please to suggest the appropriate requirements such that my job becomes easier as much as possible.
The last solution you have suggested for an applet looks the one that Iam going to use.
I would be grateful for you if you send me the code of the servers and the code that I need to add to my applet in order to do the Uploading process successfully.
Please tell me the steps to do the experiment for the fist time to guarantee that every thing is going to work. I prefer that you send me both , the code that is going to be used if we are using three hosts ,and the code for only two hosts ( the client and the database server)in order to do a starting experiment.
second code for doing the experiment is more extremely important for now.
I want you also to know that my project have another problems to be solved , and I need the current one to be solved as much fast as possible.
Thank you very much,
Jimi.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gawd ! I forgot.
I presume that you want the applet to read the image/video file from the local system (HOST-A) and then upload the file. Am I right? If so - then bad news. By default the SecurityManager will NOT allow the applet to read from the local file system !! We need to give permissions- define policy file, get certificate, sign applet's JAR file, use plug-in blah blah blah. And believe me it is a headache. So back to square one.
Do you want to deploy the applet over the internet or only over an intranet? Over the intranet it is feasable to configure permissions, but not over the internet. A system administrator can configure the client-systems on the intranet- but on the internet only the end-user can configure his system- and believe me that is asking for too much. You cannot demand that each user load the huge plug-in and then do all the configuration steps to grant the relevant permissions.
If you decide to stick to applets then you will have to go through a number of steps to sign applets, define security policy etc. etc. It is not possible to state all the steps here. Books like Core Java detail all the requirements. You may want to visit www.securingjava.com/appdx-c/ www.suitable.com/Doc_CodeSigning.shtml http://java.sun.com/products/plugin/1.2/docs
And if we are configuring permissions, then we may as well give the applet permission to connect directly to HOST-B.
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,
What a bad news!!! I thought that your next reply is going to contain the code such that I can start working . any way , it is not a big problem, I want to thank you because you care about my problem.
I want to know now what if I don't wanna use an applet?
what are the available (easy) options such that I can have a user interface and I am able to upload files from the client side to a web server and then to a database server,
or initially, from the client directly to a database server.
Thanks,
Jimi.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a Frame. End users will have to download the application onto their local file system, and then run that application.
Once you decide on whether to use Frame or signed applets, you must further decide on the what client/server protocol/technology to use. I think using servlets is the safest, because proxy/firewall may prohibit ftp connections or direct socket connections.
For the servlet approach some rough starting code:-
On the client-side, implement a method like this.
public void write(String sourcefile, String destFile, String servletURL) throws IOException {
try {
URL url=new URL(servletURL+ "/" + destFile);
URLConnection con=url.openConnection();
con.setDoOutput(true);
con.connect();
OutputStream out=con.getOutputStream();
fis=new FileInputStream(file);
byte[] buf=new byte[4*1024];
int bytesRead;
while((byesRead=fis.read(buf)) !=-1) {
out.write(buf,0,bytesRead);
}
}
finally {
if(fis!=null) fis.close();
if(out!=null) out.close();
}
On the server side, run a web-server which supports servlets and deploy a Servlet eg: FileWriterServlet extends HttpServlet and implement its doPost() method as follows:-
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
try {
String file=req.getPathTranslated();
InputStream in=req.getInputStream();
FileOutputStream fos=new FileOutputStream(file);
byte[] buf=new byte[4*1024];
int bytesRead;
while((bytesRead=in.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
} finally {
if(fos!=null) fos.close();
if(in!=null) in.close();
}

[This message has been edited by Rahul Rathore (edited March 30, 2001).]
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rahul,
I am going to use Frame on the client side using your specified code .
I want to be sure about my requirements please , tell me if the picture now if clear in my mind if you don't mind:
*I am running a frame on the client side.
**The server side is a web server.
***We are not using internet protocols.
****We are implementing a servlet on the server side.
*****As a starting experiment , I can use your code as follows:
-Compile the client side code on the client machine.
-Compile the server side code(servlet)on the server machine.
-try to upload a file from the client to the server.
I would be grateful if you tell me how to deploy a servlet, and give me some technical guid lines for doing my experiment.
Is tomcat software on a web server suffecient for our case?
*I am going to notify you when it works!
Thanks,
Jimi.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are using internet protocols- the http protocol. On the client side we will supply an http url when constructing our URL and URLConnection class. Actual connection work on client-side will be done by the HttpURLConnection class. On the server-side is a web-server i.e. http protocol server. The servlet is simply augmenting the web-server, and implements the doPost() method which corresponds to POST method of Http protocol. I am opting for the http protocol because I think that is least likely to face obstruction by proxy/firewall.
You can use the Tomcat server, or Java Web Server or ... Usually servlets class files are deployed in the server_root/servlets directory where server_root is the directory where you installed your server- But check the documentation of your server for exact info.
A lot of threads are dedicated to Tomcat server, showing that it is very popular and there are a number of experts to help you on Tomcat.
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going to start working then.
Please stay on line, I am going to send you if I face any problem. If you don't mind of course!
Thanks again.
Jimi.
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please provide me with the complete code of the servlet ?
and how can I call it from a java program (client) .
Thanks,
Jimi.
 
It's a tiny ad. At least, that's what she said.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic