當前位置:首頁 » 文件管理 » java獲取ftp文件大小

java獲取ftp文件大小

發布時間: 2022-09-15 13:24:43

⑴ 用java怎麼判斷ftp上的文件大小改變了

日誌目錄:
已經架設了一個IIS下的FTP伺服器,應該如何查看連接日誌?
答:利用IIS架設的FTP伺服器不能實時看到連接的具體情況,不過可以在C:\Winnt\system32\logfiles目錄中查看到連接上FTP伺服器的IP地址、時間等日誌信息。

如何開啟日誌:
在默認FTP站點屬性對話框中,切換到「FTP站點」標簽頁,一定要確保「啟用日誌記錄」選項被選中,這樣就可以在「事件查看器」中查看FTP日誌記錄了。

我感覺,這種方法也不是實時的,你只是將輪詢文件變成了輪詢日誌,一樣的感覺。

你如果想變成實時的,就是說文件大小一發生改變就通知你。我想到的辦法是這樣的。

你再做一個系統,這個系統可以較大頻率的去輪詢日誌或者文件,因為這個系統可以架設在別的機器上,所以不會影響你系統的性能。然後在你系統中建立一個webservice,提供下載那個文件的服務,當那個系統發現更改時調用你的webservice,你就可以下載了。

我覺得這只是一種折中的辦法,而且也比較費事,如果沒有實時的要求,還是定時輪詢的好。

PS:
實話說不知道你為什麼要檢驗文件的大小,有時內容改變不一定大小會變得。

你可以記錄文件上一次的修改時間。然後當這個發生改變時再取就是了。

⑵ Linux下如何獲取FTP文件名和大小

path='' #這里的$1,換成你需要的路徑名,或者你不換,通過shell的參數傳入
for dirlist in $(find ${path} -name *.xml)
do
echo ${dirlist}
查找,刪除,比較都在這做
done

⑶ 如何用java獲取網路文件的大小

你好,這邊有一個示例代碼,希望對你有所幫助。示例中的urlString,你可以下載之後看看是否跟列印信息大小一致。我這邊是一致的。

p:所導入的包都是java.net下面的。

main方法中 直接調用這個函數即可。

staticintgetNetWorkFile(){
StringurlString="https://img-blog.csdn.net/20180323154952670?watermark/2/text/==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70";
intlength=0;
URLurl;
try{
url=newURL(urlString);
HttpURLConnectionurlcon=(HttpURLConnection)url.openConnection();//打開連接
//根據響應獲取文件大小
length=urlcon.getContentLength();
urlcon.disconnect();//關閉連接
}catch(Exceptione){
e.printStackTrace();
}
System.out.println(length);
returnlength;
}

⑷ 如何獲取ftp伺服器下指定文件的大小

調用格式: 〈整數型〉 對象.取目錄列表 ([文本型 欲列出的文件],[文本型變數/變數數組 存放文件名的數組變數],[整數型變數/變數數組 存放文件屬性的數組變數],[長整數型變數/變數數組 存放文件尺寸的數組變數],[日期時間型變數/變數數組 存放文件時間的數組變數]) - 網路傳送支持庫->FTP輔助對象
英文名稱:FtpGetDirList
返回FTP伺服器上指定目錄內的所有匹配文件和子目錄信息。成功返回被找到的文件和子目錄的數目,失敗返回 -1失敗的原因有可能是FTP伺服器的目錄結構暫時不被支持。本命令為初級對象成員命令。
參數<1>的名稱為「欲列出的文件」,類型為「文本型(text)」,可以被省略。可以包含路徑,可以使用多字元 (*) 和單字元 (?) 通配符來指定多重文件及目錄。
參數<2>的名稱為「存放文件名的數組變數」,類型為「文本型(text)」,可以被省略,提供參數數據時只能提供變數及變數數組。提供參數數據時只能提供變數數組。在命令執行完畢後,本變數數組內被順序填入所找到的匹配文件和子目錄名稱。變數數組內原有數據被全部銷毀,變數數組的維數被自動調整為所找到的文件數目。
參數<3>的名稱為「存放文件屬性的數組變數」,類型為「整數型(int)」,可以被省略,提供參數數據時只能提供變數及變數數組。當前不被支持,留做以後擴展。
參數<4>的名稱為「存放文件尺寸的數組變數」,類型為「長整數型(int64)」,可以被省略,提供參數數據時只能提供變數及變數數組。提供參數數據時只能提供變數數組。在命令執行完畢後,本變數數組內被順序填入所找到的匹配文件的尺寸,並與文件名數組成員一一對應。
參數<5>的名稱為「存放文件時間的數組變數」,類型為「日期時間型(date)」,可以被省略,提供參數數據時只能提供變數及變數數組。提供參數數據時只能提供變數數組。在命令執行完畢後,本變數數組內被順序填入所找到的匹配文件的最後修改時間,並與文件名數組成員一一對應。變數數組內原有數據被全部銷毀,變數數組的維數被自動調整為所找到的文件數目。

⑸ java獲取ftp文件路徑怎麼寫

package com.weixin.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import com.weixin.constant.DownloadStatus;
import com.weixin.constant.UploadStatus;

/**
* 支持斷點續傳的FTP實用類
* @version 0.1 實現基本斷點上傳下載
* @version 0.2 實現上傳下載進度匯報
* @version 0.3 實現中文目錄創建及中文文件創建,添加對於中文的支持
*/
public class ContinueFTP {
public FTPClient ftpClient = new FTPClient();

public ContinueFTP(){
//設置將過程中使用到的命令輸出到控制台
this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
}

/**
* 連接到FTP伺服器
* @param hostname 主機名
* @param port 埠
* @param username 用戶名
* @param password 密碼
* @return 是否連接成功
* @throws IOException
*/
public boolean connect(String hostname,int port,String username,String password) throws IOException{
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding("GBK");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
return true;
}
}
disconnect();
return false;
}

/**
* 從FTP伺服器上下載文件,支持斷點續傳,上傳百分比匯報
* @param remote 遠程文件路徑
* @param local 本地文件路徑
* @return 上傳的狀態
* @throws IOException
*/
public DownloadStatus download(String remote,String local) throws IOException{
//設置被動模式
ftpClient.enterLocalPassiveMode();
//設置以二進制方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result;

//檢查遠程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(remote.getBytes("GBK"),"iso-8859-1"));
if(files.length != 1){
System.out.println("遠程文件不存在");
return DownloadStatus.Remote_File_Noexist;
}

long lRemoteSize = files[0].getSize();
File f = new File(local);
//本地存在文件,進行斷點下載
if(f.exists()){
long localSize = f.length();
//判斷本地文件大小是否大於遠程文件大小
if(localSize >= lRemoteSize){
System.out.println("本地文件大於遠程文件,下載中止");
return DownloadStatus.Local_Bigger_Remote;
}

//進行斷點續傳,並記錄狀態
FileOutputStream out = new FileOutputStream(f,true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=localSize /step;
int c;
while((c = in.read(bytes))!= -1){
out.write(bytes,0,c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
System.out.println("下載進度:"+process);
//TODO 更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if(isDo){
result = DownloadStatus.Download_From_Break_Success;
}else {
result = DownloadStatus.Download_From_Break_Failed;
}
}else {
OutputStream out = new FileOutputStream(f);
InputStream in= ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=0;
long localSize = 0L;
int c;
while((c = in.read(bytes))!= -1){
out.write(bytes, 0, c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
System.out.println("下載進度:"+process);
//TODO 更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
boolean upNewStatus = ftpClient.completePendingCommand();
if(upNewStatus){
result = DownloadStatus.Download_New_Success;
}else {
result = DownloadStatus.Download_New_Failed;
}
}
return result;
}

/**
* 上傳文件到FTP伺服器,支持斷點續傳
* @param local 本地文件名稱,絕對路徑
* @param remote 遠程文件路徑,使用/home/directory1/subdirectory/file.ext 按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創建不存在的目錄結構
* @return 上傳結果
* @throws IOException
*/
public UploadStatus upload(String local,String remote) throws IOException{
//設置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
//設置以二進制流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
UploadStatus result;
//對遠程目錄的處理
String remoteFileName = remote;
if(remote.contains("/")){
remoteFileName = remote.substring(remote.lastIndexOf("/")+1);
//創建伺服器遠程目錄結構,創建失敗直接返回
if(CreateDirecroty(remote, ftpClient)==UploadStatus.Create_Directory_Fail){
return UploadStatus.Create_Directory_Fail;
}
}

//檢查遠程是否存在文件
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName.getBytes("GBK"),"iso-8859-1"));
if(files.length == 1){
long remoteSize = files[0].getSize();
File f = new File(local);
long localSize = f.length();
if(remoteSize==localSize){
return UploadStatus.File_Exits;
}else if(remoteSize > localSize){
return UploadStatus.Remote_Bigger_Local;
}

//嘗試移動文件內讀取指針,實現斷點續傳
result = uploadFile(remoteFileName, f, ftpClient, remoteSize);

//如果斷點續傳沒有成功,則刪除伺服器上文件,重新上傳
if(result == UploadStatus.Upload_From_Break_Failed){
if(!ftpClient.deleteFile(remoteFileName)){
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, f, ftpClient, 0);
}
}else {
result = uploadFile(remoteFileName, new File(local), ftpClient, 0);
}
return result;
}
/**
* 斷開與遠程伺服器的連接
* @throws IOException
*/
public void disconnect() throws IOException{
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}

/**
* 遞歸創建遠程伺服器目錄
* @param remote 遠程伺服器文件絕對路徑
* @param ftpClient FTPClient對象
* @return 目錄創建是否成功
* @throws IOException
*/
public UploadStatus CreateDirecroty(String remote,FTPClient ftpClient) throws IOException{
UploadStatus status = UploadStatus.Create_Directory_Success;
String directory = remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"),"iso-8859-1"))){
//如果遠程目錄不存在,則遞歸創建遠程伺服器目錄
int start=0;
int end = 0;
if(directory.startsWith("/")){
start = 1;
}else{
start = 0;
}
end = directory.indexOf("/",start);
while(true){
String subDirectory = new String(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else {
System.out.println("創建目錄失敗");
return UploadStatus.Create_Directory_Fail;
}
}

start = end + 1;
end = directory.indexOf("/",start);

//檢查所有目錄是否創建完畢
if(end <= start){
break;
}
}
}
return status;
}

/**
* 上傳文件到伺服器,新上傳和斷點續傳
* @param remoteFile 遠程文件名,在上傳之前已經將伺服器工作目錄做了改變
* @param localFile 本地文件File句柄,絕對路徑
* @param processStep 需要顯示的處理進度步進值
* @param ftpClient FTPClient引用
* @return
* @throws IOException
*/
public UploadStatus uploadFile(String remoteFile,File localFile,FTPClient ftpClient,long remoteSize) throws IOException{
UploadStatus status;
//顯示進度的上傳
long step = localFile.length() / 100;
long process = 0;
long localreadbytes = 0L;
RandomAccessFile raf = new RandomAccessFile(localFile,"r");
OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"),"iso-8859-1"));
//斷點續傳
if(remoteSize>0){
ftpClient.setRestartOffset(remoteSize);
process = remoteSize /step;
raf.seek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
int c;
while((c = raf.read(bytes))!= -1){
out.write(bytes,0,c);
localreadbytes+=c;
if(localreadbytes / step != process){
process = localreadbytes / step;
System.out.println("上傳進度:" + process);
//TODO 匯報上傳狀態
}
}
out.flush();
raf.close();
out.close();
boolean result =ftpClient.completePendingCommand();
if(remoteSize > 0){
status = result?UploadStatus.Upload_From_Break_Success:UploadStatus.Upload_From_Break_Failed;
}else {
status = result?UploadStatus.Upload_New_File_Success:UploadStatus.Upload_New_File_Failed;
}
return status;
}

public static void main(String[] args) {
ContinueFTP myFtp = new ContinueFTP();
try {
System.err.println(myFtp.connect("10.10.6.236", 21, "5", "jieyan"));
// myFtp.ftpClient.makeDirectory(new String("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.changeWorkingDirectory(new String("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.makeDirectory(new String("愛你等於愛自己".getBytes("GBK"),"iso-8859-1"));
// System.out.println(myFtp.upload("E:\\yw.flv", "/yw.flv",5));
// System.out.println(myFtp.upload("E:\\愛你等於愛自己.mp4","/愛你等於愛自己.mp4"));
//System.out.println(myFtp.download("/愛你等於愛自己.mp4", "E:\\愛你等於愛自己.mp4"));
myFtp.disconnect();
} catch (IOException e) {
System.out.println("連接FTP出錯:"+e.getMessage());
}
}
}

⑹ 如何獲取ftp伺服器下指定文件的大小

//////獲取ftp伺服器上指定文件夾的文件列表(包含文件大小)//////////////////publicDictionaryGetFTPList(stringServerIP,stringUSERID,stringPassWord,stringpath){Dictionarydic=newDictionary();if(path==null)path="";FtpWebRequestreqFtp;try{reqFtp=(FtpWebRequest)FtpWebRequest.Create(newUri("ftp://"+ServerIP+"/"+path));reqFtp.KeepAlive=false;reqFtp.UseBinary=true;//指定ftp數據傳輸類型為二進制reqFtp.Credentials=newNetworkCredential(USERID,PassWord);//設置於ftp通訊的憑據reqFtp.Method=WebRequestMethods.Ftp.ListDirectoryDetails;//指定操作方式WebResponseresponse=reqFtp.GetResponse();//獲取一個FTP響應StreamReaderreader=newStreamReader(response.GetResponseStream(),Encoding.GetEncoding("GB2312"));//讀取響應流stringline=reader.ReadLine();while(line!=null){if(line!="."&&line!=".."){intend=line.LastIndexOf('');intstart=line.IndexOf("");stringfilename=line.Substring(end+1);if(filename.Contains(".")){line=line.Replace(filename,"");dic.Add(filename.Trim(),int.Parse(line.Substring(start).Trim()));}}line=reader.ReadLine();}}catch(Exceptionex){Console.WriteLine(ex.Message);}returndic;}文件夾或者某一文件都適用

⑺ java做一個FTP下載器,怎麼獲取伺服器上的文件及文件夾,以及他們的屬性,如文件名,時間,大小

在FTP中有FTPClient和FTPFile兩個類,里邊提供了相應的方法!你可以去試一下!

⑻ 用java怎麼獲取ftp上的文件

public class FtpClientUtil {
FtpClient ftpClient;
private String server;
private int port;
private String userName;
private String userPassword;

public FtpClientUtil(String server,int port,String userName,String userPassword)
{
this.server=server;
this.port=port;
this.userName=userName;
this.userPassword=userPassword;
}
/**
* 鏈接到伺服器
* @return
*/
public boolean open()
{
if(ftpClient!=null&&ftpClient.serverIsOpen())
return true;
try
{
ftpClient= new FtpClient();
ftpClient.openServer(server,port);
ftpClient.login(userName, userPassword);
ftpClient.binary();
return true;
}
catch(Exception e)
{
e.printStackTrace();
ftpClient=null;
return false;
}
}

public boolean cd(String dir){
boolean f = false;
try {
ftpClient.cd(dir);
} catch (IOException e) {
Logs.error(e.toString());
return f;
}
return true;
}

/**
* 上傳文件到FTP伺服器
* @param localPathAndFileName 本地文件目錄和文件名
* @param ftpFileName 上傳後的文件名
* @param ftpDirectory FTP目錄如:/path1/pathb2/,如果目錄不存在回自動創建目錄
* @throws Exception
*/
public boolean upload(String localDirectoryAndFileName,String ftpFileName,String ftpDirectory)throws Exception {
if(!open())
return false;
FileInputStream is=null;
TelnetOutputStream os=null;
try
{
char ch = ' ';
if (ftpDirectory.length() > 0)
ch = ftpDirectory.charAt(ftpDirectory.length() - 1);
for (; ch == '/' || ch == '\\'; ch = ftpDirectory.charAt(ftpDirectory.length() - 1))
ftpDirectory = ftpDirectory.substring(0, ftpDirectory.length() - 1);

int slashIndex = ftpDirectory.indexOf(47);
int backslashIndex = ftpDirectory.indexOf(92);
int index = slashIndex;
String dirall = ftpDirectory;
if (backslashIndex != -1 && (index == -1 || index > backslashIndex))
index = backslashIndex;
String directory = "";
while (index != -1) {
if (index > 0) {
String dir = dirall.substring(0, index);
directory = directory + "/" + dir;
ftpClient.sendServer("XMKD " + directory + "\r\n");
ftpClient.readServerResponse();
}
dirall = dirall.substring(index + 1);
slashIndex = dirall.indexOf(47);
backslashIndex = dirall.indexOf(92);
index = slashIndex;
if (backslashIndex != -1 && (index == -1 || index > backslashIndex))
index = backslashIndex;
}
ftpClient.sendServer("XMKD " + ftpDirectory + "\r\n");
ftpClient.readServerResponse();

os = ftpClient.put(ftpDirectory + "/"
+ ftpFileName);
File file_in = new File(localDirectoryAndFileName);
is = new FileInputStream(file_in);
byte bytes[] = new byte[1024];
int i;
while ((i = is.read(bytes)) != -1)
os.write(bytes, 0, i);
//清理垃圾

return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();
}
}
/**
* 從FTP伺服器上下載文件並返回下載文件長度
* @param ftpDirectoryAndFileName
* @param localDirectoryAndFileName
* @return
* @throws Exception
*/
public long download(String ftpDirectoryAndFileName,String localDirectoryAndFileName)throws Exception
{
long result = 0;
if(!open())
return result;
TelnetInputStream is = null;
FileOutputStream os = null;
try
{
is = ftpClient.get(ftpDirectoryAndFileName);
java.io.File outfile = new java.io.File(localDirectoryAndFileName);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
result = result + c;
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();

}
return result;
}
/**
* 返回FTP目錄下的文件列表
* @param ftpDirectory
* @return
*/
public List<String> getFileNameList(String ftpDirectory)
{
List<String> list = new ArrayList<String>();
if(!open())
return list;
try
{
DataInputStream dis = new DataInputStream(ftpClient.nameList(ftpDirectory));
String filename = "";
while((filename=dis.readLine())!=null)
{
list.add(filename);
}
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* 刪除FTP上的文件
* @param ftpDirAndFileName
*/
public boolean deleteFile(String ftpDirAndFileName)
{
if(!open())
return false;
ftpClient.sendServer("DELE "+ftpDirAndFileName+"\r\n");
return true;
}
/**
* 刪除FTP目錄
* @param ftpDirectory
*/
public boolean deleteDirectory(String ftpDirectory)
{
if(!open())
return false;
ftpClient.sendServer("XRMD "+ftpDirectory+"\r\n");
return true;
}
/**
* 關閉鏈接
*/
public void close()
{
try
{
if(ftpClient!=null&&ftpClient.serverIsOpen())
ftpClient.closeServer();
}catch(Exception e)
{

}
}
}望採納,謝謝。

⑼ java從FTP上下載文件大小大概10M左右,ftpClient.setBufferSize(1024)要設置多大的呢

幾k到1m 這個是臨時的緩沖

⑽ 如何獲取上傳到ftp服務端的文件大小

可以用原始指令 size [path]filename 如果文件存在 返迴文件長度 否則返回5xx 失敗

熱點內容
sql臨時表效率 發布:2024-10-10 22:24:37 瀏覽:502
linux搭建mc伺服器搭建 發布:2024-10-10 21:58:38 瀏覽:983
ubuntu解壓工具 發布:2024-10-10 21:58:27 瀏覽:230
c語言一組標識符 發布:2024-10-10 21:37:38 瀏覽:747
安卓游戲機在哪裡下載 發布:2024-10-10 21:23:48 瀏覽:461
網上買了伺服器怎麼在電腦連接失敗怎麼辦 發布:2024-10-10 21:18:35 瀏覽:963
安卓息屏壁紙在哪個文件夾 發布:2024-10-10 21:11:58 瀏覽:407
eer資料庫 發布:2024-10-10 21:06:08 瀏覽:709
ftp匿名用戶上傳 發布:2024-10-10 21:06:06 瀏覽:754
凱越原始密碼多少 發布:2024-10-10 21:06:04 瀏覽:193