java實(shí)現(xiàn)ftp文件上傳下載功能
本文實(shí)例為大家分享了ftp實(shí)現(xiàn)文件上傳下載的具體代碼,供大家參考,具體內(nèi)容如下
package getUrlPic;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUploadFile {
public static void main(String[] args){
// public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
// boolean success = false;
FTPClient ftp = new FTPClient();
InputStream input = null;
try {
int reply;
ftp.connect("localhost", 21);//連接FTP服務(wù)器
//如果采用默認(rèn)端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
ftp.login("test", "test");//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("can not connect");
// return success;
}else{
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftp.changeWorkingDirectory(path);
input = new ByteArrayInputStream("中xuxxx".getBytes("utf-8"));
ftp.storeFile("test.txt", input);
// 創(chuàng)建目錄
ftp.makeDirectory("/test/bb");
//列出目錄
FTPFile[] dirs = ftp.listDirectories("/test");
for(FTPFile f : dirs ){
System.out.println(f.getName());
}
}
// ftp.changeWorkingDirectory(path);
// ftp.storeFile(filename, input);
// input.close();
// ftp.logout();
// success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if(input != null){
try{
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
// return success;
}
// }
}
參考:
JAVA中使用FTPClient實(shí)現(xiàn)文件上傳下載實(shí)例代碼
java實(shí)現(xiàn)ftp上傳 如何創(chuàng)建文件夾
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)貪心算法的實(shí)現(xiàn)
本文主要介紹了Java數(shù)據(jù)結(jié)構(gòu)貪心算法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2007-03-03
關(guān)于Spring中Bean的創(chuàng)建進(jìn)行更多方面的控制
今天小編就為大家分享一篇關(guān)于關(guān)于Spring中Bean的創(chuàng)建進(jìn)行更多方面的控制,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
java實(shí)現(xiàn)TCP socket和UDP socket的實(shí)例
這篇文章主要介紹了本文主要介紹了java實(shí)現(xiàn)TCP socket和UDP socket的實(shí)例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Springboot @Configuration與自動配置詳解
這篇文章主要介紹了SpringBoot中的@Configuration自動配置,在進(jìn)行項(xiàng)目編寫前,我們還需要知道一個東西,就是SpringBoot對我們的SpringMVC還做了哪些配置,包括如何擴(kuò)展,如何定制,只有把這些都搞清楚了,我們在之后使用才會更加得心應(yīng)手2022-07-07
Java中關(guān)鍵字final finally finalize的區(qū)別介紹
這篇文章主要給大家分享的是 Java中final,finally,finalize 到底有什么區(qū)別,文章圍繞final,finally,finalize的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考的價(jià)值,需要的朋友可以參考一下2022-04-04

