Solved

FTP STOR COMMAND USING JAVA

Posted on 1997-12-06
2
381 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 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

17 Experts available now in Live!

Get 1:1 Help Now