Solved

FTP STOR COMMAND USING JAVA

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 76
Adding multiple JVM environments to RedHat 6 7 81
What browser will run Java? 7 171
What does this method description mean? 1 7
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

739 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