Solved

upload file to FTP

Posted on 2009-05-12
12
732 Views
Last Modified: 2012-08-14
hi All,
         i'm trying to upload a file to FTP server. but i'm not able to upload and could not any err message. please verify mycode and help me

strucking at this line        
OutputStream os= cconn.openUploadStream(targetfile); // STRUCKING HERE

IF not can u plz some links code snippets for upload to FTP

FTPupload.java

=======================

import java.io.File;

import java.io.FileInputStream;

import java.io.OutputStream;
 
 

public class FTPupload {

    protected FTPclientConn cconn;

    public final String localfile;

    public final String targetfile;
 

    public FTPupload(String _host, String _user, String _password,

         String _localfile, String _targetfile) {

  cconn= new FTPclientConn(_host, _user, _password);

  localfile= _localfile;

  targetfile= _targetfile;

  doit();

    }

    public FTPupload(String _host, String _user, String _password, String _file) {

  cconn= new FTPclientConn(_host, _user, _password);

  localfile= _file;

  targetfile= _file;

  doit();

    }
 

    protected void doit() {

  try {
 

       OutputStream os= cconn.openUploadStream(targetfile); // STRUCKING HERE
 

       FileInputStream is=  new FileInputStream(localfile);

      byte[] buf= new byte[16384];

      int c;

      while (true) {

    //System.out.print(".");

    c= is.read(buf);

    if (c<= 0)  break;

    //System.out.print("[");

    os.write(buf, 0, c);

    //System.out.print("]");

      }

      os.close();

      is.close();

      cconn.close(); // section 3.2.5 of RFC1738

  } catch (Exception E) {

      System.err.println(E.getMessage());

      E.printStackTrace();

  }

    }
 

    public static void main(String args[]) {

  // Usage: FTPupload host, user, password, file

//  new FTPupload(args[0], args[1], args[2], args[3]);

     new FTPupload("xxx.xx.xx.xx:21","UN","PWD","C:\\Test.txt","Upload\\test.txt");  	

    }
 

}
 
 
 
 
 
 

FTPClientConn.java

================================
 

import java.io.*;

import java.net.*;
 
 

class FTPclientConn {

    public final String host;

    public final String user;

    protected final String password;

    protected URLConnection urlc;
 

    public FTPclientConn(String _host, String _user, String _password) {

  host= _host;  user= _user;  password= _password;

  urlc = null;

    }

    protected URL makeURL(String targetfile) throws MalformedURLException  {

  if (user== null)

      return new URL("ftp://"+ host+ "/"+ targetfile+ ";type=i");

  else

      return new URL("ftp://"+ user+ ":"+ password+ "@"+ host+ "/"+ targetfile+ ";type=i");

    }
 

    protected InputStream openDownloadStream(String targetfile) throws Exception {

  URL url= makeURL(targetfile);

  urlc = url.openConnection();

  InputStream is = urlc.getInputStream();

  return is;

    }
 

    protected OutputStream openUploadStream(String targetfile) throws Exception {

  URL url= makeURL(targetfile);

  urlc = url.openConnection();

  OutputStream os = urlc.getOutputStream();

  return os;

    }
 

    protected void close() {

  urlc= null;

    }

}

Open in new window

0
Comment
Question by:websyn
  • 6
  • 4
  • 2
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24362997
>      new FTPupload("xxx.xx.xx.xx:21","UN","PWD","C:\\Test.txt","Upload\\test.txt");    


try:

     new FTPupload("xxx.xx.xx.xx:21","UN","PWD","C:\\Test.txt","Upload/test.txt");    

0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24363001
If this is not part of a homework or something similar, why reinvent the wheel?

Thry this allready finished FTP Client for java from the apache guys:
http://commons.apache.org/net/
0
 
LVL 92

Expert Comment

by:objects
ID: 24363003
commons net provides a nicer ftp client if you're interested

http://commons.apache.org/net/

0
 

Author Comment

by:websyn
ID: 24363289
getting this err
java.io.FileNotFoundException: CWD devt2009: 550 devt2009: The system cannot find the file specified.
0
 
LVL 92

Expert Comment

by:objects
ID: 24363302
check your paths are correct and the required directory exists

0
 

Author Comment

by:websyn
ID: 24363321
Path is correct and able to upload from my FTP client but not thru program
0
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.

 
LVL 92

Expert Comment

by:objects
ID: 24363338
same user and relative path?

whats the devt2009 directory it is referring to?

0
 
LVL 92

Expert Comment

by:objects
ID: 24363343
acually thats an absolute path you are using

0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24363344
I think your approach is not going to lead you to the result you want.
I couldn't find any of the FTP protocol communication. All you do, is to connect to a certain host and port and then sent all the data ...

The URLConnection does only the simplest of the simple processing. I doubt it implements the FTP protocol specified here: http://www.ietf.org/rfc/rfc959.txt

Here is a link on best practices regarding Java and FTP. They state your current approach only works in some conditions and is propably going to fail in most cases:
http://www.nsftools.com/tips/JavaFtp.htm
0
 

Author Comment

by:websyn
ID: 24363498
@Objects
for absolute n relative paths getting same err.
0
 

Author Comment

by:websyn
ID: 24370737
Can anyone Please give the working sample
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24370769
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

Title # Comments Views Activity
What is the latest versions eclipse neon 2 140
java  and programming certification ? 4 86
maven project error 5 48
more than one jdk and one jre 1 34
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

895 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

15 Experts available now in Live!

Get 1:1 Help Now