?
Solved

FTP STOR COMMAND USING JAVA

Posted on 1997-12-06
2
Medium Priority
?
459 Views
Last Modified: 2010-07-27
null
0
Comment
Question by:bradd120697
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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