日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java上传文件到ftp_java实现文件上传下载至ftp服务器

發(fā)布時間:2024/4/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java上传文件到ftp_java实现文件上传下载至ftp服务器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以前做的一個項目,用到了文件上傳下載至ftp服務(wù)器,現(xiàn)在對其進(jìn)行一下復(fù)習(xí),比較簡單,一下就能看明白。

環(huán)境:首先,先安裝ftp服務(wù)器,我是在win8本地用IIS配置的, 百度一下就可以找到安裝文檔。

1.在你的項目目錄下建立ftp配置文件,目錄如下圖

01 ftpconfig.properties:

ftpIp=10.73.222.29

ftpPort=21

ftpUser=WP

ftpPwd=04143114wp

ftpRemotePath=d://share

02 讀取ftpconfig.properties中的具體內(nèi)容的類:

package com.java.core.util;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

/**

* @author wangpei

* @version 創(chuàng)建時間:2017年5月6日 下午9:42:40 讀取ftp文件的配置文件

*/

public class ReadFtpProperties {

private InputStream is;

private Properties properties;

public ReadFtpProperties() {

is = this.getClass().getResourceAsStream("/ftpconfig.properties");// 將配置文件讀入輸入流中

properties = new Properties();

try {

properties.load(is);

} catch (IOException e) {

System.out.println("配置文件不存在..");

e.printStackTrace();

} finally {

if (null != is) {

try {

is.close();

} catch (IOException e) {

System.out.println("關(guān)閉流失敗..");

e.printStackTrace();

}

}

}

}

public String getIp() {// 獲取ftp服務(wù)器的ip地址

return properties.getProperty("ftpIp");

}

public String getPort() {// 獲取ftp服務(wù)器的端口

return properties.getProperty("ftpPort");

}

public String getUser() {// 獲取ftp登錄用戶名

return properties.getProperty("ftpUser");

}

public String getPwd() {// 獲取ftp服務(wù)器的登錄密碼

return properties.getProperty("ftpPwd");

}

public String getRemotePath() {// 獲取ftp服務(wù)器的存放文件的目錄

return properties.getProperty("ftpRemotePath");

}

}

03 文件上傳下載的接口類

package com.java.web.service;

import java.io.InputStream;

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

import com.java.core.util.ReadFtpProperties;

/**

* @author wangpei

* @version 創(chuàng)建時間:2017年5月6日 下午6:39:03

* 文件上傳下載業(yè)務(wù)邏輯接口層

*/

public interface FtpService {

/*

* 登錄至FTP

*/

public boolean loginFTP(FTPClient client, ReadFtpProperties rfp);

/*

* 退出ftp

*/

public boolean logout(FTPClient client);//

/*

* 上傳文件到remotePath,其在ftp上的名字為inputStream

*/

public boolean uploadFile(FTPClient client, String remotePath,

String fileNewName, InputStream inputStream, ReadFtpProperties rfp);

/*

* 從目錄remotePath,下載文件fileName

*/

public InputStream downFileByFtp(FTPClient client, String remotePath,

String fileName);

/*

* 刪除ftp上的目錄為pathName的文件

*/

public boolean delFile(FTPClient client, String pathName);

}

04 文件上傳下載的接口實現(xiàn)類

package com.java.web.service.serviceImpl;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;

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

import org.apache.commons.net.ftp.FTPFile;

import com.java.core.util.ReadFtpProperties;

import com.java.web.service.FtpService;

/**

* @author wangpei

* @version 創(chuàng)建時間:2017年5月6日 下午10:02:28 類說明

*/

public class FtpServiceImpl implements FtpService {

public boolean loginFTP(FTPClient client, ReadFtpProperties rfp) {

String ftpIp = rfp.getIp();

String ftpPort = rfp.getPort();

String ftpUser = rfp.getUser();

String ftpPwd = rfp.getPwd();

// String fgtpRemotePath = rfp.getRemotePath();

boolean b = false;

try {

client.connect(ftpIp, Integer.parseInt(ftpPort));

} catch (NumberFormatException e) {

System.out.println("無法連接到ftp");

return false;

} catch (SocketException e) {

System.out.println("無法連接到ftp");

return false;

} catch (IOException e) {

System.out.println("無法連接到ftp");

return false;

}

client.setControlEncoding("uft-8");

try {

b = client.login(ftpUser, ftpPwd);

} catch (IOException e) {

System.out.println("登錄ftp出錯");

logout(client);// 退出/斷開FTP服務(wù)器鏈接

return false;

}

return b;

}

public boolean logout(FTPClient client) {

boolean b = false;

try {

b = client.logout();// 退出登錄

client.disconnect();// 斷開連接

} catch (IOException e) {

return false;

}

return b;

}

public boolean uploadFile(FTPClient client, String remotePath,

String fileNewName, InputStream inputStream, ReadFtpProperties rfp) {

boolean b = false;

try {

client.setFileType(FTPClient.BINARY_FILE_TYPE);

client.enterLocalPassiveMode();

if (remotePath != null && !"".equals(remotePath.trim())) {

String[] pathes = remotePath.split("/");

for (String onepath : pathes) {

if (onepath == null || "".equals(onepath.trim())) {

continue;

}

onepath = new String(onepath.getBytes("utf-8"),

"iso-8859-1");

System.out.println("onepath=" + onepath);

if (!client.changeWorkingDirectory(onepath)) {

client.makeDirectory(onepath);// 創(chuàng)建FTP服務(wù)器目錄

client.changeWorkingDirectory(onepath);// 改變FTP服務(wù)器目錄

} else {

System.out.println("文件單路徑");

}

}

}

b = client.storeFile(new String(fileNewName.getBytes("utf-8"),

"iso-8859-1"), inputStream);

} catch (UnsupportedEncodingException e) {

return false;

} catch (IOException e) {

return false;

}

return b;

}

public InputStream downFileByFtp(FTPClient ftpClient, String remotePath,

String fileName) {

FTPFile[] fs;

InputStream is = null;

try {

// 設(shè)置被動模式

ftpClient.enterLocalPassiveMode();

// 設(shè)置以二進(jìn)制流的方式傳輸

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

// 設(shè)置編輯格式

ftpClient.setControlEncoding("utf-8");

remotePath = remotePath.substring(0,

remotePath.lastIndexOf(fileName));

fs = ftpClient.listFiles(remotePath);// 遞歸目標(biāo)目錄

for (FTPFile ff : fs) {

if (ff.getName().equals(fileName)) {// 查找目標(biāo)文件

is = ftpClient.retrieveFileStream(new String(

(remotePath + fileName).getBytes("utf-8"),

"iso-8859-1"));

break;

}

}

} catch (IOException e) {

e.printStackTrace();

}

return is;

}

public boolean delFile(FTPClient ftpClient, String pathName) {

boolean b = false;

try {

b = ftpClient.deleteFile(pathName);

return b;

} catch (Exception e) {

return false;

} finally {

logout(ftpClient);// 退出/斷開FTP服務(wù)器鏈接

}

}

}

代碼很好理解,看一遍應(yīng)該就可以理解,在這兒就不具體分析了,主要看代碼中的注釋。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的java上传文件到ftp_java实现文件上传下载至ftp服务器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。