Java Authoring ftp Download tool _java

Source: Internet
Author: User
Tags getstream

Need to use Java to write an FTP tool, because only a little Java foundation, but because a few years do not, almost is not, had to do a little bit, fortunately can pick up.

However, because of the use of Javac in Linux to compile, not in WIN under the IDE to do these things, so in the run and compile a little time, but it is because of some of the JAVA compiler, the operation of the knowledge to understand some.

For the FTP download tool, the code is as follows:

Copy Code code as follows:

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.net.SocketException;
Import org.apache.commons.net.ftp.FTPClient;
Import org.apache.commons.net.ftp.FTPReply;
public class FtpClient {
Private String host;
private int port;
Private String username;
private String password;
Private Boolean binarytransfer = true;
Private Boolean passivemode = true;
Private String encoding = "UTF-8";
private int clientTimeout = 3000;
Private Boolean flag=true;
Private ftpclient ftpclient = null;
Public String GetHost () {
return host;
}
public void Sethost (String host) {
This.host = host;
}
public int Getport () {
return port;
}
public void Setport (int port) {
This.port = port;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
public Boolean isbinarytransfer () {
return binarytransfer;
}
public void Setbinarytransfer (Boolean binarytransfer) {
This.binarytransfer = Binarytransfer;
}
public Boolean Ispassivemode () {
return passivemode;
}
public void Setpassivemode (Boolean passivemode) {
This.passivemode = Passivemode;
}
Public String getencoding () {
return encoding;
}
public void setencoding (String encoding) {
this.encoding = encoding;
}
public int getclienttimeout () {
return clientTimeout;
}
public void setclienttimeout (int clientTimeout) {
This.clienttimeout = ClientTimeout;
}
Public ftpclient (String Host) {
This.username = "Anonymous";
this.encoding = "Utf-8";
This.binarytransfer = true;
This.binarytransfer = true;
This.port = 21;
This.host = host;
try {
This.ftpclient = Getftpclient ();
catch (Exception e) {
System.out.println ("Create ftpclient error!");
}
}
Private FtpClient Getftpclient () throws IOException {
FtpClient ftpclient = new FtpClient ();
ftpclient.setcontrolencoding (encoding);
Connect (ftpclient);
if (Passivemode) {
Ftpclient.enterlocalpassivemode ();
}
Setfiletype (ftpclient);
try {
Ftpclient.setsotimeout (clientTimeout);
catch (SocketException e) {
throw new IOException ("Set timeout error.", e);
}
return ftpclient;
}
private void Setfiletype (FtpClient ftpclient) throws IOException {
try {
if (Binarytransfer) {
Ftpclient.setfiletype (Ftpclient.binary_file_type);
} else {
Ftpclient.setfiletype (Ftpclient.ascii_file_type);
}
catch (IOException e) {
throw new IOException ("Could not to set file type.", e);
}
}
public boolean connect (ftpclient ftpclient) throws IOException {
try {
Ftpclient.connect (host, Port);
int reply = Ftpclient.getreplycode ();
if (ftpreply.ispositivecompletion (reply)) {
if (Ftpclient.login (username, password)) {
Setfiletype (ftpclient);
return true;
}
} else {
This.ftpClient.disconnect ();
throw new IOException ("FTP server refused connection.");
}
catch (IOException e) {
if (this.ftpClient.isConnected ()) {
try {
This.ftpClient.disconnect ();
catch (IOException E1) {
throw new IOException ("Could not disconnect from server.", e);
}
}
throw new IOException ("Could not connect to server.", e);
}
return false;
}
private void Disconnect () throws IOException {
try {
This.ftpClient.logout ();
catch (IOException e) {
System.out.println ("Logout may timeout!");
finally {
if (this.ftpClient.isConnected ()) {
This.ftpClient.disconnect ();
}
}
}
Public InputStream GetStream (String serverfile) throws IOException {
InputStream instream = null;
try {
instream = This.ftpClient.retrieveFileStream (serverfile);
System.out.println ("Instream get over!");
return instream;
catch (IOException e) {
System.out.println ("Get stream exception");
return null;
}
}
public boolean Writestream (InputStream input, String localfile) throws IOException {
FileOutputStream fout = new FileOutputStream (localfile);
int ch = 0;
if (input = = null) {
SYSTEM.OUT.PRINTLN ("input is null");
return false;
}
try {
ch = input.read ();
while (Ch!=-1) {
Fout.write (CH);
ch = input.read ();
}
System.out.println ("Write over!");
return flag;
catch (IOException e) {
throw new IOException ("couldn ' t get file from server.", e);
}
}
public boolean isexist (String remotefilepath) throws ioexception{
try{
File File=new file (Remotefilepath);
String remotepath=remotefilepath.substring (0, (Remotefilepath.indexof (File.getname ())-1);
string[] ListNames = This.ftpClient.listNames (RemotePath);
System.out.println (Remotefilepath);
for (int i=0;i<listnames.length;i++) {
System.out.println (Listnames[i]);
if (Remotefilepath.equals (Listnames[i])) {
Flag=true;
System.out.println ("File:" +file.getname () + "existed");
Break
}else {
Flag=false;
}
}
catch (IOException e) {
throw new IOException ("FILE EXCEPTION", e);
}
return flag;
}
Main for testing
public static void Main (string[] args) throws IOException {
String hostname = "cp01-testing-ps7130.cp01.baidu.com";
String serverfile= "/home/work/check_disk.sh";
String localfile= "/home/work/workspace/project/dhc2-0/dhc/base/ftp/task_get";
ftpclient ftp = new FtpClient (hostname);
System.out.println (Ftp.isexist (serverfile));
Ftp.writestream (Ftp.getstream (serverfile), localfile);
Ftp.disconnect ();
}
}

This tool is designed to work with another Hadoop tool for cluster uploads, so the input and output streams are separated from each other to facilitate the use of another tool.

To add, how to run on a Linux configuration:

If such code needs to run under Linux, first configure the response package, for example

Copy Code code as follows:

Import org.apache.commons.net.ftp.FTPClient;

This package on the Apache Web site directly downloaded on the line, after decompression to find the corresponding jar package, at compile time to quote:

Copy Code code as follows:

Export ftppath= "${path}/xxx.jar"
Javac-classpath $CLASSPATH: $FTPPATH Ftpclient.java

Also, specify classpath at run time:

Copy Code code as follows:

Java-classpath $CLASSPATH: $FTPPATH ftpclient

It is recommended not to include $ftppath in the CLASSPATH, what kind of package to reference the environment variables on the line, there is no need to add a brain, just as we do not have to import all the package.

The above mentioned is the entire content of this article, hope to be able to learn Java to help you.

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.