Solved

FTP STOR COMMAND USING JAVA

Posted on 1997-12-06
2
395 Views
Last Modified: 2010-07-27
null
0
Comment
Question by:bradd120697
2 Comments
 
LVL 1

Expert Comment

by:fadl
ID: 1231521
I can send you my code that does the job.
All commands including RETR,STOR work for me,
so it might be usefull for you... Just send me your
e-mail address.

I don't want to post such a long source file here.

Michal
fadl@corpus.cz
0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 100 total points
ID: 1231522
The following snippet of code should get you on your way. If you need more info, check out the jftp class at:

http://www.iit.edu/~harejoh/jftp/jftp_home.html

it contains a full FTP client app plus source.

Hope this helps.

public boolean sendFile(boolean allowOverrite, String fileName) {

    ServerSocket serverSocket = null;
    Socket clientSocket = null;
    OutputStream outdataport;
    String command;
    int result;
    int amount;


    if(State != CONNECTED) {
      printText("ERR Not connected, unable to send file!\n");
      return(false);
    }

    try {
      serverSocket = new ServerSocket(0);
    }
    catch (IOException e) {
      printText("ERR Could not get port for listening: " +
            serverSocket.getLocalPort() + ", " + e);
      return(false);
    }

    setType("BINARY");
    setPort(serverSocket);

    // ok, send command      
    if(allowOverrite) {
      command = "STOR " + fileName + "\r";
    } else {
      command = "STOU " + fileName + "\r";
    }
    controlOutputStream.println(command);
    printText(command + "\n");
    result = checkResponse();

    if(result == POSITIVE_PRELIMINARY_REPLY) {

      // listen on data port
      try {
      clientSocket = serverSocket.accept();
      } catch (IOException e) {
      printText("ERR Accept failed: " +
              serverSocket.getLocalPort() + ", " + e + "\n");
      return(false);
      }

      try {
      outdataport = clientSocket.getOutputStream();

      byte b[] = new byte[1024];  // 1K blocks I guess

        // Get the current directory on the local machine
        String curDir = System.getProperty("user.dir");
        String newFile = new String(curDir + File.separatorChar + fileName);

      // open file
      RandomAccessFile infile = new RandomAccessFile(newFile, "r");

      // Note: read returns 0 at EOF (API says -1)
      while ((amount = infile.read(b)) > 0) {
        outdataport.write(b, 0, amount);
        printText("-");
      }
      printText("\n");

      // clean up when done
      infile.close();
      outdataport.close();
      clientSocket.close();
      serverSocket.close();

      result = checkResponse();

      } catch (IOException e) {
      printText("ERR " + e.toString() + "\n" );
      return(false);
      }

      return(result == POSITIVE_COMPLETION_REPLY);
          
    } else {
      printText("ERR while trying to send file.\n");
      try {
      serverSocket.close();
      }
      catch (IOException e) {
      printText("ERR closing server socket.\n");
      return(false);
      }
      return(false);
    }
  }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now