Solved

FTP STOR COMMAND USING JAVA

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Glassfish admin console not working 1 56
Html Table Looping (part 2) 5 35
What is the use of Forwarding Class in java 1 34
How to fix  socket closed error 11 27
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

756 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