devxlogo

Using Preexisting Software to Write User Data to a File

Using Preexisting Software to Write User Data to a File

Question:
I am trying to write a Java applet which canretrieve data from the user and then write itto a file on the server. Unfortunately I do nothave the access to create a program on myserver 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) onmy server to accomplish this task?

Answer:
Unfortunately, no. At the minimum, you’d need access to aCGI script or other server program on your server to get the dataand write it to a file.

Using ftp to store files onto your server is a good idea, but itwon’t work with applets because of the way the ftp protocol worksand the restrictions imposed by the applet security model.

Java does indeed come with all the necessary libraries for performingftp operations to and from the server. This functionality isprovided in one of Java’s networking packages called sun.net.ftp.Included in this package is an ftpClient class that allows Java applicationsto 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 ofJDK1.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 URLsof the form “ftp://java.sun.com/some/file/name”. In general,the sun.* packages provide the underlying implementationfor the public Java APIs, and are themselves available to Javaprogrammers. However, because they are not part of the publishedJava API, they may change from release to release.
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email

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 writea string to the given filename on the server. Here’s how one mightinvoke it:

java ftpPut myserver testfile.txtimport 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 afile to the server using ftp, the server uses a pull modelto retrieve data from the client. This means the client would have tocreate a ServerSocket and listen for pull requests coming from the server.However, creating ServerSockets is considered to be a security violationby the applet security manager because it could be abused by a maliciousapplet to turn an unsuspecting user’s system into an arbitrary serverand potentially cause a denial of service attack. As a result, ifan applet were to try the stunt above, it would get a security exceptionand the ftp operation would fail.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist