java ftp文件大小_java如何获得ftp服务器上指定文件的大小

我想获得ftp服务器上指定文件的大小

以及下载过程中文件的已完成下载的大小

求教

这个是我写的方法

code::

public static boolean downLoadFtp(String ftpServerIp, String ftpUser,

String ftpPassWord, String url, String filePath, String fileName) {

try {

FtpClient fc = new FtpClient(ftpServerIp);

fc.login(ftpUser, ftpPassWord);

fc.binary();

int ch;

int filesize = 0;

String fileStr = filePath + fileName;

File filename = new File(fileStr);

System.out.println("C::     download   89    filename size   ::  "+filename.toString());

if (filename.exists()) {//如果存在就直接覆盖

filename.createNewFile();

//return false;

}

if (!filename.exists()) {

if (filename.createNewFile()) {

System.out.println("File   is   created   successfully!");

}

}

RandomAccessFile getFile = new RandomAccessFile(fileStr, "rw");

getFile.seek(0);

// url="/FTPTest1.txt";

TelnetInputStream fget = fc.get(fileName);

DataInputStream puts = new DataInputStream(fget);

while ((ch = puts.read()) >= 0) {

filesize = filesize + ch;

getFile.write(ch);

}

System.out.println("C::   download   96    filesize  :   :   "

+ filesize);

fget.close();

getFile.close();

fc.closeServer();

} catch (IOException ex) {

ex.printStackTrace();

}

return true;

}

我应该如何在下载之前获得文件的大小啊

以及在现在过程中下载文件的大小···

谢谢··

我想做一个进度条····

可是没有办法获得文件的大小····

谢谢····

2009年3月18日 17:07

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了一些类和API来实现FTP文件服务器。以下是一个简单的FTP文件服务器的示例代码: ```java import java.io.*; import java.net.*; import org.apache.commons.net.ftp.*; public class FTPServer { public static void main(String[] args) { FTPServer server = new FTPServer(); server.start(); } public void start() { int port = 21; try { ServerSocket serverSocket = new ServerSocket(port); System.out.println("FTP Server started on port " + port); while (true) { Socket clientSocket = serverSocket.accept(); System.out.println("Accepted connection from " + clientSocket.getInetAddress().getHostAddress()); FTPConnection connection = new FTPConnection(clientSocket); connection.start(); } } catch (IOException ex) { System.err.println("Error starting FTP server: " + ex.getMessage()); } } } class FTPConnection extends Thread { private Socket clientSocket; private FTPServer server; private FTPClient ftp; public FTPConnection(Socket clientSocket) { this.clientSocket = clientSocket; this.server = server; ftp = new FTPClient(); } public void run() { try { ftp.connect(clientSocket.getInetAddress().getHostName()); ftp.login("anonymous", ""); ftp.enterLocalPassiveMode(); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); String line = null; out.print("220 FTP Server ready.\r\n"); out.flush(); while ((line = in.readLine()) != null) { if (line.startsWith("USER")) { out.print("331 Password required for " + line.substring(5) + "\r\n"); out.flush(); } else if (line.startsWith("PASS")) { out.print("230 User logged in.\r\n"); out.flush(); } else if (line.startsWith("QUIT")) { out.print("221 Goodbye.\r\n"); out.flush(); break; } else if (line.startsWith("TYPE")) { out.print("200 Command okay.\r\n"); out.flush(); } else if (line.startsWith("LIST")) { FTPFile[] files = ftp.listFiles(); out.print("150 Opening ASCII mode data connection for file list.\r\n"); out.flush(); for (FTPFile file : files) { out.print(file.getName() + "\r\n"); out.flush(); } out.print("226 Transfer complete.\r\n"); out.flush(); } else if (line.startsWith("RETR")) { out.print("150 Opening BINARY mode data connection for " + line.substring(5) + "\r\n"); out.flush(); String fileName = line.substring(5); OutputStream os = clientSocket.getOutputStream(); ftp.retrieveFile(fileName, os); out.print("226 Transfer complete.\r\n"); out.flush(); } else { out.print("502 Command not implemented.\r\n"); out.flush(); } } ftp.disconnect(); clientSocket.close(); } catch (IOException ex) { System.err.println("Error handling FTP connection: " + ex.getMessage()); } } } ``` 这个例子使用Apache Commons Net库来实现FTP连接和文件传输。它可以处理一些基本的FTP命令,如USER、PASS、LIST和RETR。你可以根据需要添加更多的功能和命令来实现一个更完整的FTP文件服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值