javaftp下載所有文件
這個要做定時任務的,ftp不可能主動給你發,只能自己每隔多長時間就去檢索一次,應該把ftp文件目錄結構和文件名稱全部存入資料庫,在下載時候對文件的標識狀態位陵森進行更新,方便於對文件的判斷。然後從ftp下載文件即可。如果需尺汪陪要連陵蠢接ftp下載文件的代碼,可以發送郵件到[email protected]
㈡ java如何獲取ftp制定目錄下所有文件集合(包括文件名稱)只要一個方法。
/**
* 取得相對於當前連接目錄的某個目錄下所有文件列表
*
* @param path
* @return
*/
public List getFileList(String path){
List list = new ArrayList();
DataInputStream dis;
try {
dis = new DataInputStream(ftpClient.nameList(this.path + path));
String filename = "";
while((filename = dis.readLine()) != null){
list.add(filename);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
我從這里拷來的 你不清楚看看裡面 http://hi..com/yuanhotel/item/000b6334894d11f42784f4da
滿意就採納 謝謝
㈢ java ftp連接一次下載多個文件
/**
*參考例子如下:
*@paramnamelist下載的文件列表
*@parampath下載路徑
*@paramzipname壓縮文件名稱
*/
publicvoidzipDownloadFile(HttpServletResponseresponse,List<string>namelist,Stringpath,Stringzipname){
byte[]buf=newbyte[1024];
try{
//本地保存設置
response.addHeader("Content-Disposition","attachment;filename="
+URLEncoder.encode(zipname,sysEncoding)+".zip");
response.setContentType("application/x-zip-compressed");
//向本地寫文件
ServletOutputStreamsos=response.getOutputStream();
ZipOutputStreamzipOut=newZipOutputStream(newBufferedOutputStream(sos));
for(Stringname:namelist){
ZipEntryentry=newZipEntry(name);
zipOut.putNextEntry(entry);
InputStreambis=this.getStream(path,name);
if(bis!=null){
intreadLen=-1;
while((readLen=bis.read(buf,0,1024))!=-1){
zipOut.write(buf,0,readLen);
}
bis.close();
}
}
zipOut.close();
}catch(Exceptione){
e.printStackTrace();
}
}</string>
㈣ ftp上的中文路徑的文件怎麼用java下載
正則表達式是十分有用,但是彎基櫻在使用時也要付出代價。尤其是在 N.O.P.E 分支深處時,要不惜一切代碼鋒纖避免使用正則表達式。還要小心各種使用到正則表達式的JDK字元串方法,比如 String.replaceAll() 或 String.split()。可以選擇用比較流行的埋叢開發庫,比如 Apache Commons Lang 來進行字元串操作。
㈤ java如何實現txt下載 就是從遠程FTP伺服器上直接把TXT文本下載下來!
strUrl為文件地址,fileName為文件在本地的保存路徑,試試吧~
public static void writeFile(String strUrl, String fileName) {
URL url = null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e2) {
e2.printStackTrace();
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e1) {
e1.printStackTrace();
}
OutputStream os = null;
try {
os = new FileOutputStream( fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
System.out.println("下載成功:"+strUrl);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
㈥ java FTP下載文件在代碼中如何實現知道下載完成
(KmConfigkmConfig,StringfileName,StringclientFileName,OutputStreamoutputStream){
try{
StringftpHost=kmConfig.getFtpHost();
intport=kmConfig.getFtpPort();
StringuserName=kmConfig.getFtpUser();
StringpassWord=kmConfig.getFtpPassword();
Stringpath=kmConfig.getFtpPath();
FtpClientftpClient=newFtpClient(ftpHost,port);//ftpHost為FTP伺服器的IP地址,port為FTP伺服器的登陸埠,ftpHost為String型,port為int型。
ftpClient.login(userName,passWord);//userName、passWord分別為FTP伺服器的登陸用戶名和密碼
ftpClient.binary();
ftpClient.cd(path);//path為FTP伺服器上保存上傳文件的路徑。
try{
TelnetInputStreamin=ftpClient.get(fileName);
byte[]bytes=newbyte[1024];
intcnt=0;
while((cnt=in.read(bytes,0,bytes.length))!=-1){
outputStream.write(bytes,0,cnt);
}
//##############################################
//這里文件就已經下載完了,自己理解一下
//#############################################
outputStream.close();
in.close();
}catch(Exceptione){
ftpClient.closeServer();
e.printStackTrace();
}
ftpClient.closeServer();
}catch(Exceptione){
System.out.println("下載文件失敗!請檢查系統FTP設置,並確認FTP服務啟動");
}
}
㈦ java ftp批量下載異常
我之前也遇到過這樣的事,通過FTP獲取文件的二進制流有限制,獲取第二個流的時候需要斷掉鏈接後再重新連接伺服器讀取流
㈧ java FTP下載
檢查一下是否timeout時間設置過短。不要設置內存或者處理器限制。 還有在IIS的metabase資料庫中找一下FTP的設置,在那裡找配置文件修改最直接。
通過CFtpFileFind 得到文件的URL之後,然後通過CHttpFile::QueryInfo 得到文件大小。
求採納為滿意回答。
㈨ java ftp上批量下載。。。如何和本地文件對應比較。。
比較文件名、比較文件大小,或者比較文件位元組序列。最絕的方法是比較文件的MD5值!
㈩ 請教Java Jsp中Ftp文件下載問題
<%@ page contentType="text/html; charset=GBK"%>
<%@ page language="java" import="java.io.IOException" pageEncoding="GBK"%>
<%@ page import="com.enterprisedt.net.ftp.FTPClient"%>
<%@ page import="com.enterprisedt.net.ftp.FTPTransferType"%>
<%@ page import="java.io.*,com.enterprisedt.net.ftp.FTPConnectMode;"%>
<html>
<head>
<title>主頁面</title>
</head>
<body>
<%
String path = null;
String filename = null;
path = request.getParameter("path");
filename = request.getParameter("filename");
//response.setContentType("application/unknown");//設置為下載application/x-download
//其中%20是空格在UTF-8下的編碼
//filename = URLEncoder.encode(filename, "UTF-8");
//filename = new String(filename.getBytes("gb2312"),"ISO8859-1");
//response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
String host = "192.168.12.49";
String users = "admin";
String passwords = "admin";
if (path.trim().length() > 1) {
path = path.trim() + "/";
}
String remoteFile = path + filename; //確定源文件的位置
String downFile = "d:/" + filename; //確定源文件的位置
System.out.println(remoteFile + " 正在下載中,請稍等................");
try {
OutputStream outputStream = response.getOutputStream();
FTPClient client = new FTPClient();
client.setRemoteHost(host);
//client.setDetectTransferMode(true);
client.connect();
client.login(users, passwords);
client.setConnectMode(FTPConnectMode.ACTIVE);
client.setType(FTPTransferType.BINARY);
//client.get(outputStream, remoteFile); //*方式一:將ftp上的文件取出後,寫入到response(outputStream),以response把文件帶到瀏覽器,由瀏覽器來提示用戶是否願意保存文件到本
//*一直存在中文文件名沒有解決的問題
client.get(downFile, remoteFile); //*方式二:將FTP上文件取出後,直接下載到D盤下
outputStream.close();
client.quit();
} catch (IOException e) {
e.printStackTrace();
}
%>
</body>
</html>