當前位置:首頁 » 文件管理 » javaapacheftp

javaapacheftp

發布時間: 2022-02-25 01:28:39

A. 如何用java實現ftp伺服器

1.使用的FileZillaServer開源免費軟體,安裝過後建立的本地FTP伺服器。2.使用的apache上下載FTP工具包,引用到工程目錄中。3.IDE,Eclipse,JDK6上傳和下載目錄的實現原理:對每一個層級的目錄進行判斷,是為目錄類型、還是文件類型。如果為目錄類型,採用遞歸調用方法,檢查到最底層的目錄為止結束。如果為文件類型,則調用上傳或者下載方法對文件進行上傳或者下載操作。貼出代碼:(其中有些沒有代碼,可以看看,還是很有用處的)!

B. java apache FTPClient上傳文件時的一個問題

源文件不變 長傳時候在文件後面加.tmp 或者你想的後綴 完成後 操作這個文件重命名 具體代碼懶得貼了

C. java 用代碼怎麼去調用windows 的ftp 伺服器下傳下載

ftp不分windows還是linux,走的是協議,apache有開源的ftp上傳下載實現,下載就可以了
在net包裡面

D. java Apache ftp下載文件內容亂碼,不是文件名亂碼,設置了[ftp.setControlEncoding("GBK");]也沒用。

我估計是你的第二張圖的那個文件的保存的編碼格式跟你設置的GBK的傳輸編碼格式不一致

E. 如何用java 程序啟動Apache-ftpserver 功能

public class FtpServerListener implements ServletContextListener {

public static final String FTPSERVER_CONTEXT_NAME = "org.apache.ftpserver";

public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Stopping FtpServer");

FtpServer server = (FtpServer) sce.getServletContext().getAttribute(FTPSERVER_CONTEXT_NAME);

if(server != null) {
server.stop();

sce.getServletContext().removeAttribute(FTPSERVER_CONTEXT_NAME);

System.out.println("FtpServer stopped");
} else {
System.out.println("No running FtpServer found");
}

}

public void contextInitialized(ServletContextEvent sce) {
System.out.println("Starting FtpServer");

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());

FtpServer server = (FtpServer) ctx.getBean("myFtpServer");

sce.getServletContext().setAttribute(FTPSERVER_CONTEXT_NAME, server);

try {
server.start();
System.out.println("FtpServer started");
} catch (Exception e) {
throw new RuntimeException("Failed to start FtpServer", e);
}
}

}

F. java做ftp伺服器需要什麼jar包

apache的包:commons-net.jar
主要使用到的類:
org.apache.commons.net.ftp.FTPClient;
org.apache.commons.net.ftp.FTPFile;
org.apache.commons.net.ftp.FTPReply;

JDK中也有自帶的ftp包:
sun.net.ftp.下有FTP操作類
但sun已經不建議使用了, 建議樓主使用apache的包

G. java怎麼在ftp上取到文件夾中文件再錄入txt文本

前段時間正好看了這個。

http://www.codejava.net/java-se/networking/ftp/java-ftp-file-download-tutorial-and-example

這個文檔非常好。有什麼看不懂的再問吧。

主要是使用org.apache.commons.net.ftp.FTPClient 和org.apache.commons.net.ftp.FTP 類。

核心代碼:

ftpClient.connect(server,port);
ftpClient.login(user,pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

//APPROACH#1:usingretrieveFile(String,OutputStream)
StringremoteFile1="/test/video.mp4";
FiledownloadFile1=newFile("D:/Downloads/video.mp4");
OutputStreamoutputStream1=newBufferedOutputStream(newFileOutputStream(downloadFile1));
booleansuccess=ftpClient.retrieveFile(remoteFile1,outputStream1);
outputStream1.close();

if(success){
System.out.println("File#.");
}

H. java如何設置apache ftpserver api 的數據埠


ftpd.bat ftpd-typical.xml

I. 如何用java連接到ftp上

現在已經封裝好了的方法,不需要任何其他知識即可連接的。只需要知道ftp登錄用戶名、密碼、埠、存儲路徑即可。
package zn.ccfccb.util;

import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

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

public class CCFCCBFTP {

/**
* 上傳文件
*
* @param fileName
* @param plainFilePath 明文文件路徑路徑
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上傳文件開始");
Log.info("連接遠程上傳伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// Log.info("連接遠程上傳伺服器"+"192.168.54.106:"+2021);
// ftpClient.connect("192.168.54.106", 2021);
// ftpClient.login("hkrt-CCFCCBHK", "3OLJheziiKnkVcu7Sigz");
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("檢查文件路徑是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查詢文件路徑不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上傳文件成功:"+fileName+"。文件保存路徑:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}

}

/**
*下載並解壓文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String SFP = System.getProperty("file.separator");
String bl = "false";
try {
Log.info("下載並解密文件開始");
Log.info("連接遠程下載伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
// ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
FTPFile[] fs;

ftpClient.makeDirectory(routeFilepath);
ftpClient.changeWorkingDirectory(routeFilepath);
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true";
Log.info("下載文件開始。");
ftpClient.setBufferSize(1024);
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
fos = new FileOutputStream(localFilePath+SFP+fileName);
fos.write(bos.toByteArray());
Log.info("下載文件結束:"+localFilePath);
}
}
Log.info("檢查文件是否存:"+fileName+" "+bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查詢無結果,請稍後再查詢。");
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}

熱點內容
安卓手機電量為什麼越充越少 發布:2024-10-27 18:24:20 瀏覽:191
linux輸出文件 發布:2024-10-27 18:22:41 瀏覽:179
久益壓縮機頭 發布:2024-10-27 18:17:10 瀏覽:543
諜影重重合集ftp 發布:2024-10-27 18:17:10 瀏覽:88
如何關閉安卓手機語音控制 發布:2024-10-27 18:12:50 瀏覽:109
win2008ftp密碼設置 發布:2024-10-27 18:02:58 瀏覽:862
left函數sql 發布:2024-10-27 18:00:47 瀏覽:520
江西省農保卡默認密碼是多少 發布:2024-10-27 17:58:13 瀏覽:575
說唱壓縮器 發布:2024-10-27 17:54:56 瀏覽:672
java位或運算符 發布:2024-10-27 17:54:16 瀏覽:917