Javaを使用してFTPツールを作成する必要があります。これはJavaの基本が少ししかないので、数年間使用していないので、ほとんどできないので、少ししなければなりませんビット、しかし幸いなことに私はそれを拾うことができます。
ただし、Javacを使用してLinuxの下でコンパイルし、IDEを使用してこれらのことをWINで行うため、実行してコンパイルするのに時間がかかりました。 。 いくつかの。
FTPダウンロードツールの場合、コードは次のとおりです。
コードコピーは次のとおりです。
java.io.fileをインポートします。
java.io.fileinputStreamをインポートします。
java.io.filenotfoundexceptionをインポートします。
java.io.fileoutputStreamをインポートします。
java.io.ioexceptionをインポートします。
java.io.inputStreamをインポートします。
java.io.outputStreamをインポートします。
java.net.socketexceptionをインポートします。
Import org.apache.commons.net.ftp.ftpclient;
import org.apache.commons.net.ftp.ftpreply;
パブリッククラスftpclient {
プライベートストリングホスト。
プライベートインターポート;
プライベート文字列ユーザー名;
プライベート文字列パスワード。
プライベートブールバイナリトランスファー= true;
private boolean passivemode = true;
プライベート文字列エンコード= "utf-8";
private int clienttimeout = 3000;
プライベートブールフラグ= true;
private ftpclient ftpclient = null;
public string gethost(){
ホストを返します。
}
public void sethost(string host){
this.host = host;
}
public int getport(){
返品ポート。
}
public void setport(int port){
this.port = port;
}
public string getUsername(){
ユーザー名を返します。
}
public void setusername(string username){
this.username = username;
}
public string getPassword(){
パスワードを返します。
}
public void setpassword(string password){
this.password = password;
}
public boolean isbinarytransfer(){
バイナリトランスファーを返します。
}
public void setbinarytransfer(boolean binary transfer){
this.binarytransfer = binary transfer;
}
public boolean ispassivemode(){
PassiveModeを返します。
}
public void setPassiveMode(boolean passivemode){
this.passiveMode = passivemode;
}
public string getEncoding(){
エンコードを返します。
}
public void setEncoding(文字列エンコード){
this.encoding = encoding;
}
public int getClientTimeout(){
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;
試す {
this.ftpclient = getFtpClient();
} catch(例外e){
system.out.println( "ftpclientエラーを作成!");
}
}
private ftpclient getftpclient()はioexceptionをスローします{
ftpclient ftpclient = new ftpclient();
ftpclient.setControlencoding(エンコード);
connect(ftpclient);
if(passivemode){
ftpclient.enterlocalpassivemode();
}
setFileType(ftpClient);
試す {
ftpclient.setsotimeout(clienttimeout);
} catch(socketexception e){
新しいioException( "Set Timeout Error。"、e)をスローします。
}
ftpclientを返します。
}
private void setFiletype(ftpclient ftpclient)はioexceptionをスローします{
試す {
if(binarytransfer){
ftpclient.setfiletype(ftpclient.binary_file_type);
} それ以外 {
ftpclient.setfiletype(ftpclient.ascii_file_type);
}
} catch(ioException e){
新しいioException( "ファイルタイプを設定できなかった。"、e)をスローします。
}
}
public boolean connect(ftpclient ftpclient)はioexceptionをスローします{
試す {
ftpclient.connect(host、port);
int Reply = ftpclient.getReplyCode();
if(ftpreply.ispositiveCompletion(reply)){
if(ftpclient.login(username、password)){
setFileType(ftpClient);
trueを返します。
}
} それ以外 {
this.ftpclient.disconnect();
新しいioException( "ftpサーバーが接続を拒否しました。");
}
} catch(ioException e){
if(this.ftpclient.isconnected()){
試す {
this.ftpclient.disconnect();
} catch(ioexception e1){
新しいioExceptionを投げます( "サーバーから切断できませんでした。"、e);
}
}
新しいioException( "Serverに接続できませんでした。"、e);
}
falseを返します。
}
private void disconnect()throws ioException {
試す {
this.ftpclient.logout();
} catch(ioException e){
system.out.println( "logout may timeout!");
} ついに {
if(this.ftpclient.isconnected()){
this.ftpclient.disconnect();
}
}
}
public inputstream getStream(String ServerFile)IOExceptionをスロー{
inputStream instream = null;
試す {
instream = this.ftpclient.retrievefilestream(serverFile);
system.out.println( "instream get over!");
戻り即興;
} catch(ioException e){
system.out.println( "stream例外を取得");
nullを返します。
}
}
public boolean writeStream(inputstream input、string localfile)はioexceptionをスローします{
fileoutputStream fout = new fileoutputStream(localfile);
int ch = 0;
if(input == null){
system.out.println( "input is null");
falseを返します。
}
試す {
ch = input.read();
while(ch!= -1){
fout.write(ch);
ch = input.read();
}
System.out.println( "書き込み!");
フラグを返します。
} catch(ioException e){
新しいioException( "サーバーからファイルを取得できませんでした。"、e);
}
}
public boolean isexist(string remotefilepath)IoException {
試す{
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()+"存在");
壊す;
}それ以外 {
flag = false;
}
}
} catch(ioException e){
新しいioException( "ファイル例外"、e)をスローします。
}
フラグを返します。
}
//テスト用のメイン
public static void main(string [] args)throws ioexception {
string hostname = "cp01-testing-ps7130.cp01.baidu.com";
文字列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();
}
}
このツールは、別のHadoopツールを使用したクラスターアップロードに使用されるため、入力ストリームと出力ストリームは分離されています。これは、別のツールの使用を容易にするためです。
1つを追加するには、Linux構成で実行する方法:
そのようなコードをLinuxで実行する必要がある場合、まず第一に、レスポンシブパッケージを構成する必要があります。たとえば
コードコピーは次のとおりです。
Import org.apache.commons.net.ftp.ftpclient;
このパッケージは、Apache Webサイトから直接ダウンロードされます。
コードコピーは次のとおりです。
ftppath = "$ {path} /xxx.jar"
Javac -ClassPath $ classPath:$ ftppath ftpclient.java
同様に、実行時にClassPathを指定する必要があります。
コードコピーは次のとおりです。
Java -ClassPath $ classPath:$ ftppath ftpclient
ClassPathに$ ftppathを含めないことをお勧めします。すべてのパッケージをインポートする必要がないように、すべてを追加する必要がない場合は、環境変数を参照してください。
上記は、この記事についてのすべてです。
記事を友達と共有するか、コメントを残してください。ご支援ありがとうございます!