中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

ftp上傳下載Java實(shí)現(xiàn)

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.util.Arrays;

import java.util.List;

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

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

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

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

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

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.XMLWriter;

public class FtpHelper {

private FTPClient ftp = null;

/**

 * Ftp服務(wù)器

 */

private String server;

/**

 * 用戶名

 */

private String uname;

/**

 * 密碼

 */

private String password;

/**

 * 連接端口,默認(rèn)21

 */

private int port = 21;

private Document document ;

public FtpHelper(String server, int port, String uname,

String password){

this.server = server;

if (this.port > 0){

this.port = port;

}

this.uname = uname;

this.password = password;

//初始化

ftp = new FTPClient();

}

/**

 * 連接FTP服務(wù)器

 * 

 * @param server

 * @param uname

 * @param password

 * @return

 * @throws Exception

 */

public FTPClient connectFTPServer() throws Exception {

try {

ftp.configure(getFTPClientConfig());

ftp.connect(this.server, this.port);

if (!ftp.login(this.uname, this.password)) {

ftp.logout();

ftp = null;

return ftp;

}

// 文件類型,默認(rèn)是ASCII

ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

ftp.setControlEncoding("GBK");

// 設(shè)置被動(dòng)模式

ftp.enterLocalPassiveMode();

//ftp.setConnectTimeout(2000);

ftp.setBufferSize(1024);

// 響應(yīng)信息

int replyCode = ftp.getReplyCode();

if ((!FTPReply.isPositiveCompletion(replyCode))) {

// 關(guān)閉Ftp連接

closeFTPClient();

// 釋放空間

ftp = null;

throw new Exception("登錄FTP服務(wù)器失敗,請(qǐng)檢查![Server:" + server + "、"

+ "User:" + uname + "、" + "Password:" + password);

} else {

return ftp;

}

} catch (Exception e) {

ftp.disconnect();

ftp = null;

throw e;

}

}

/**

 * 配置FTP連接參數(shù)

 * 

 * @return

 * @throws Exception

 */

public FTPClientConfig getFTPClientConfig() throws Exception {

String systemKey = FTPClientConfig.SYST_NT;

String serverLanguageCode = "zh";

FTPClientConfig conf = new FTPClientConfig(systemKey);

conf.setServerLanguageCode(serverLanguageCode);

conf.setDefaultDateFormatStr("yyyy-MM-dd");

return conf;

}

/**

 * 向FTP根目錄上傳文件

 * 

 * @param localFile

 * @param newName

 *            新文件名

 * @throws Exception

 */

public Boolean uploadFile(String localFile, String newName)

throws Exception {

InputStream input = null;

boolean success = false;

try {

File file = null;

if (checkFileExist(localFile)) {

file = new File(localFile);

}

input = new FileInputStream(file);

success = ftp.storeFile(newName, input);

if (!success) {

throw new Exception("文件上傳失敗!");

}

} catch (Exception e) {

throw e;

} finally {

if (input != null) {

input.close();

}

}

return success;

}

/**

 * 向FTP根目錄上傳文件

 * 

 * @param input

 * @param newName

 *            新文件名

 * @throws Exception

 */

public Boolean uploadFile(InputStream input, String newName)

throws Exception {

boolean success = false;

try {

success = ftp.storeFile(newName, input);

if (!success) {

throw new Exception("文件上傳失敗!");

}

} catch (Exception e) {

throw e;

} finally {

if (input != null) {

input.close();

}

}

return success;

}

/**

 * 向FTP指定路徑上傳文件

 * 

 * @param localFile

 * @param newName

 *            新文件名

 * @param remoteFoldPath

 * @throws Exception

 */

public Boolean uploadFile(String localFile, String newName,

String remoteFoldPath) throws Exception {

InputStream input = null;

boolean success = false;

try {

File file = null;

if (checkFileExist(localFile)) {

file = new File(localFile);

}

input = new FileInputStream(file);

// 改變當(dāng)前路徑到指定路徑

if (!this.changeDirectory(remoteFoldPath)) {

System.out.println("服務(wù)器路徑不存!");

return false;

}

success = ftp.storeFile(newName, input);

if (!success) {

throw new Exception("文件上傳失敗!");

}

} catch (Exception e) {

throw e;

} finally {

if (input != null) {

input.close();

}

}

return success;

}

/**

 * 向FTP指定路徑上傳文件

 * 

 * @param input

 * @param newName

 *            新文件名

 * @param remoteFoldPath

 * @throws Exception

 */

public Boolean uploadFile(InputStream input, String newName,

String remoteFoldPath) throws Exception {

boolean success = false;

try {

// 改變當(dāng)前路徑到指定路徑

if (!this.changeDirectory(remoteFoldPath)) {

System.out.println("服務(wù)器路徑不存!");

return false;

}

success = ftp.storeFile(newName, input);

if (!success) {

throw new Exception("文件上傳失敗!");

}

} catch (Exception e) {

throw e;

} finally {

if (input != null) {

input.close();

}

}

return success;

}

/**

 * 從FTP服務(wù)器下載文件

 * 

 * @param remotePath

 *            FTP路徑(不包含文件名)

 * @param fileName

 *            下載文件名

 * @param localPath

 *            本地路徑

 */

public Boolean downloadFile(String remotePath, String fileName,

String localPath) throws Exception {

BufferedOutputStream output = null;

boolean success = false;

try {

// 檢查本地路徑

this.checkFileExist(localPath);

// 改變工作路徑

if (!this.changeDirectory(remotePath)) {

System.out.println("服務(wù)器路徑不存在");

return false;

}

// 列出當(dāng)前工作路徑下的文件列表

List<FTPFile> fileList = this.getFileList();

if (fileList == null || fileList.size() == 0) {

System.out.println("服務(wù)器當(dāng)前路徑下不存在文件!");

return success;

}

for (FTPFile ftpfile : fileList) {

if (ftpfile.getName().equals(fileName)) {

File localFilePath = new File(localPath + File.separator

+ ftpfile.getName());

output = new BufferedOutputStream(new FileOutputStream(

localFilePath));

success = ftp.retrieveFile(ftpfile.getName(), output);

}

}

if (!success) {

throw new Exception("文件下載失敗!");

}

} catch (Exception e) {

throw e;

} finally {

if (output != null) {

output.close();

}

}

return success;

}

/**

 * 從FTP服務(wù)器獲取文件流

 * 

 * @param remoteFilePath

 * @return

 * @throws Exception

 */

public InputStream downloadFile(String remoteFilePath) throws Exception {

return ftp.retrieveFileStream(remoteFilePath);

}

/**

 * 獲取FTP服務(wù)器上指定路徑下的文件列表

 * 

 * @param filePath

 * @return

 */

public List<FTPFile> getFtpServerFileList(String remotePath)

throws Exception {

FTPListParseEngine engine = ftp.initiateListParsing(remotePath);

List<FTPFile> ftpfiles = Arrays.asList(engine.getNext(25));

return ftpfiles;

}

/**

 * 獲取FTP服務(wù)器上[指定路徑]下的文件列表

 * 

 * @param path

 * @return

 * @throws Exception

 */

public List<FTPFile> getFileList(String remotePath) throws Exception {

List<FTPFile> ftpfiles = Arrays.asList(ftp.listFiles(remotePath));

return ftpfiles;

}

/**

 * 獲取FTP服務(wù)器[當(dāng)前工作路徑]下的文件列表

 * 

 * @param path

 * @return

 * @throws Exception

 */

public List<FTPFile> getFileList() throws Exception {

List<FTPFile> ftpfiles = Arrays.asList(ftp.listFiles());

return ftpfiles;

}

/**

 * 改變FTP服務(wù)器工作路徑 

 * 

 * @param remoteFoldPath

 */

public Boolean changeDirectory(String remoteFoldPath) throws Exception {

return ftp.changeWorkingDirectory(remoteFoldPath);

}

/**

 * 刪除文件

 * 

 * @param remoteFilePath

 * @return

 * @throws Exception

 */

public Boolean deleteFtpServerFile(String remoteFilePath) throws Exception {

return ftp.deleteFile(remoteFilePath);

}

/**

 * 創(chuàng)建目錄

 * 

 * @param remoteFoldPath

 * @return

 */

public boolean createFold(String remoteFoldPath) throws Exception {

boolean flag = ftp.makeDirectory(remoteFoldPath);

if (!flag) {

throw new Exception("創(chuàng)建目錄失敗");

}

return false;

}

/**

 * 刪除目錄

 * @param remoteFoldPath

 * @return

 * @throws Exception

 */

public boolean deleteFold(String remoteFoldPath) throws Exception {

return ftp.removeDirectory(remoteFoldPath) ;

}

/**

 * 刪除目錄以及文件

 * 

 * @param remoteFoldPath

 * @return

 */

public boolean deleteFoldAndsubFiles(String remoteFoldPath)

throws Exception {

boolean success = false;

List<FTPFile> list = this.getFileList(remoteFoldPath);

if (list == null || list.size() == 0) {

return deleteFold(remoteFoldPath);

}

for (FTPFile ftpFile : list) {

String name = ftpFile.getName();

if (ftpFile.isDirectory()) {

success = deleteFoldAndsubFiles(remoteFoldPath + "/" + name);

if (!success)

break;

} else {

success = deleteFtpServerFile(remoteFoldPath + "/" + name);

if (!success)

break;

}

}

if (!success)

return false;

success = deleteFold(remoteFoldPath);

return success;

}

/**

 * 檢查本地路徑是否存在

 * 

 * @param filePath

 * @return

 * @throws Exception

 */

public boolean checkFileExist(String filePath) throws Exception {

boolean flag = false;

File file = new File(filePath);

if (!file.exists()) {

throw new Exception("本地路徑不存在,請(qǐng)檢查!");

} else {

flag = true;

}

return flag;

}

/**

 * 創(chuàng)建XML文件

 * @return

 */

public Element getCurrentElement(){

document = DocumentHelper.createDocument();

return document.addElement("root");

}

/**

 * 生成目錄XML文件

 */

public void createDirectoryXML(String remotePath,Element fatherElement) throws Exception{

List<FTPFile> list = this.getFileList();

for(FTPFile ftpfile:list){

Element currentElement = fatherElement; //當(dāng)前的目錄節(jié)點(diǎn)

String newRemotePath = remotePath+ftpfile.getName();

if(ftpfile.isDirectory()){

Element dirElement = fatherElement.addElement("dir") ;

dirElement.addAttribute("name",ftpfile.getName());

currentElement = dirElement;

this.changeDirectory(newRemotePath); //從根目錄開始

createDirectoryXML(newRemotePath,dirElement);

}else{

 Element fileElement = fatherElement.addElement("file");//文件節(jié)點(diǎn)

 fileElement.setText(ftpfile.getName()) ;

}

}

}

/**

 * 保存xml

 */

public void saveXML(){

XMLWriter output = new XMLWriter();

        //輸出格式化

        OutputFormat format = OutputFormat.createPrettyPrint();

        try {

            output = new XMLWriter(new FileWriter("src/com/shine/Ftp/config/dir.xml"), format);

            output.write(this.document);

            output.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

}

/**

 * 關(guān)閉FTP連接

 * 

 * @param ftp

 * @throws Exception

 */

public void closeFTPClient(FTPClient ftp) throws Exception {

try {

if (ftp.isConnected())

ftp.logout();

ftp.disconnect();

} catch (Exception e) {

throw new Exception("關(guān)閉FTP服務(wù)出錯(cuò)!");

}

}

/**

 * 關(guān)閉FTP連接

 * 

 * @throws Exception

 */

public void closeFTPClient() throws Exception {

this.closeFTPClient(this.ftp);

}

/**

 * Get Attribute Method

 * 

 */

public FTPClient getFtp() {

return ftp;

}

public String getServer() {

return server;

}

public String getUname() {

return uname;

}

public String getPassword() {

return password;

}

public int getPort() {

return port;

}

/**

 * Set Attribute Method

 * 

 */

public void setFtp(FTPClient ftp) {

this.ftp = ftp;

}

public void setServer(String server) {

this.server = server;

}

public void setUname(String uname) {

this.uname = uname;

}

public void setPassword(String password) {

this.password = password;

}

public void setPort(int port) {

this.port = port;

}

/**

 * 主方法(測(cè)試)

 * 

 * 問題:上傳時(shí)命名的新文件名不能有中文,否則上傳失敗.

 * 

 * @param args

 */

public static void main(String[] args) {

try {

FtpHelper fu = new FtpHelper("192.168.2.18", 21, "administrator","sunshine");

fu.connectFTPServer();

Element fatherElement = fu.getCurrentElement();

fu.createDirectoryXML("/", fatherElement);

fu.saveXML();

} catch (Exception e) {

System.out.println("異常信息:" + e.getMessage());

}

}

}

標(biāo)簽: ftp服務(wù)器 ftp服務(wù)器下載 isp 服務(wù)器 連接ftp服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:mysqldump 把數(shù)據(jù)庫(kù)備份到異地的服務(wù)器

下一篇:JS把指定日期轉(zhuǎn)換為幾秒前、幾小時(shí)前等格式