Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy file to FTP server

Status
Not open for further replies.

gozdmei

Programmer
Jul 24, 2003
3
TR
Hi there, I need to copy a file from my computer (windows 2000) to an FTP site. I need to find a java code to perform this. If you can help me, I will be pleased. Thank you.
 
Hi!

Try this and read the java API to understand what the code does. You will need some smarter exception handling. ;-)

Code:
import java.net.*;
import java.io.*;

public class FTPTest
{


 public static void main(String [] arstring) 
 {
  String user      = "myFTPuser";
  String password  = "myFTPpassword";
  String host      = "myFTPhost";
  String file      = "myFTPfile";
  String userInfo  = user + ":" + password;
  String urlString = "ftp://" + userInfo + "@" + host + "/" + file + ";type=i";
  int bufferSize = 1024;
  try{
   URL url = new URL(urlString);
   URLConnection urlc = url.openConnection();
   OutputStream os = urlc.getOutputStream();
   BufferedOutputStream ost = new BufferedOutputStream(os, bufferSize);
   FileInputStream ist = new FileInputStream(file);
   byte[] buffer = new byte[bufferSize];
   int nBytes;
   while((nBytes = ist.read(buffer)) != -1)
   {
  	ost.write(buffer, 0, nBytes);
  	ost.flush();
   }
  }
  catch(Exception e){
  	e.printStackTrace();
  }
 }
}



Cheers

frag


patrick.metz@epost.de
 
sorry, there is something "wrong" in my code.
use type=i if it is a binary file (an image for example) and use type=a if you want to transfer an ascii file (text file).

Cheers

frag

patrick.metz@epost.de
 
There is some undocumented stuff in the "sun.net" package:
I tried a bit, but didn't get it to work. Maybe you can if you change server, user and passwd.
(ps : the microsoft site does not need a user/password, but I do't know how to login anonymous)
Code:
import sun.net.ftp.*;
import sun.net.TelnetInputStream;

public class FtpExample {

  public FtpExample() {
  }

  private void doIt() {
    String server = "ftp.microsoft.com";
    String user = "me";
    String passwd = "1234";
    try {
      FtpClient client = new FtpClient();
      client.openServer(server);
      client.login(user, passwd);
      client.binary();
      client.cd("MISC");
      TelnetInputStream in = client.get("ReadMe1.txt");//now do whatever u want with this inputstream
      System.out.println("ReadMe1 = " + in.toString());
      in.close();
      client.closeServer();
    } catch (java.io.IOException e) {
    }

  }

  public static void main(String[] args) {
    FtpExample example = new FtpExample();
    example.doIt();
  }

}
 
I tried Frag's code but I got the following exception:

"protocol doesn't support output"

I don't have any permission or authenticity problems because when I open the ftp site with my browser with the same user I used in my code , I can change a file on the server or put a new file to the server.
 
Hologram, I tried your code and it works!!! I tried TelnetOutputStream too...

try {
ftpClient client = new ftpClient();
client.openServer(server);
client.login(user, passwd);
client.binary();
client.cd("MISC");
File file_in= new File("c:\\readme.txt");
FileInputStream is= new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
int total_bytes=0;
while((c=is.read(bytes)) !=-1)
{
total_bytes +=c;
is.read(bytes,0,c);
}

TelnetOutputStream out=client.put(readme.txt);
out.write(total_bytes);
out.flush();
out.close();
client.closeServer();
}


Thank you very much fellas, I really appreciate. Now I will work on frag's code.
 
my code worked in my environment... don't know what could be wrong with it, sorry.

patrick.metz@epost.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top