Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Applets
Expertise: Beginner
Mar 20, 1997

Using Preexisting Software to Write User Data to a File

Question:
I am trying to write a Java applet which can retrieve data from the user and then write it to a file on the server. Unfortunately I do not have the access to create a program on my server to take the data and write it to a file.

Is there a way I can use pre-existing software (such as an ftp program) on my server to accomplish this task?

Answer:
Unfortunately, no. At the minimum, you'd need access to a CGI script or other server program on your server to get the data and write it to a file.

Using ftp to store files onto your server is a good idea, but it won't work with applets because of the way the ftp protocol works and the restrictions imposed by the applet security model.

Java does indeed come with all the necessary libraries for performing ftp operations to and from the server. This functionality is provided in one of Java's networking packages called sun.net.ftp. Included in this package is an ftpClient class that allows Java applications to ftp to a server, log in as a user, get and put files, change directories, and so on.

Here's what the sun.net.ftp.ftpClient package looks like as of JDK1.0.2:

Constructors:

 ftpClient(String)
       New ftp client connected to host.
  ftpClient(String, int)
       New ftp client connected to host host, port port.
  ftpClient()
       Create an uninitialized ftp client.
 
Methods:
ascii()
       Set transfer type to 'A'
  binary()
       Set transfer type to 'I'
  cd(String)
       CD to a specific directory on a remote ftp server
  closeServer()
       issue the QUIT command to the ftp server and close the connection.
  get(String)
       GET a file from the ftp server
  issueCommand(String)
  issueCommandCheck(String)
  list()
       LIST files on a remote ftp server
  login(String, String)
       login user to a host with username user and password password
  openDataConnection(String)
  openServer(String)
       open a ftp connection to host host.
  openServer(String, int)
       open a ftp connection to host host on port port.
  put(String)
       PUT a file to the ftp server
  readReply()
 
In fact, Sun's HotJava browser uses this package to implement URLs of the form "ftp://java.sun.com/some/file/name". In general, the sun.* packages provide the underlying implementation for the public Java APIs, and are themselves available to Java programmers. However, because they are not part of the published Java API, they may change from release to release.

Here's how one would use this package. The following Java application (not applet) takes two command line arguments: a hostname and a filename. It will ftp to the given hostname, log in as anonymous and write a string to the given filename on the server. Here's how one might invoke it:

java ftpPut myserver testfile.txt


import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import sun.net.ftp.*;

public class ftpPut {

       public ftpPut(String hostname, String filename) {
               try {
                       ftpClient ftp = new ftpClient(hostname);
                       ftp.login("anonymous", "foo@bar");
                       ftp.cd("pub");
                       OutputStream out = ftp.put(filename);
                       PrintStream p = new PrintStream(out);
                       p.println("This is a test");
                       p.close();
                       out.close();
               } catch (Exception err) {
                       System.out.println("ftp failed: " + err.toString());
                       err.printStackTrace();
               }
       }

       public static void main(String argv[]) {
               ftpPut put = new ftpPut(argv[0], argv[1]);
       }

}
 
This stunt will not work with applets, however. When you transfer a file to the server using ftp, the server uses a pull model to retrieve data from the client. This means the client would have to create a ServerSocket and listen for pull requests coming from the server. However, creating ServerSockets is considered to be a security violation by the applet security manager because it could be abused by a malicious applet to turn an unsuspecting user's system into an arbitrary server and potentially cause a denial of service attack. As a result, if an applet were to try the stunt above, it would get a security exception and the ftp operation would fail.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date