java ftp输出流_Apache下的FTP上传文件,使用FTPClient的appendFileStream方法返回输出流为空?望高手指点。急!!!!...

望高手指点。拜谢!/***上传文件到服务器,新上传和断点续传*@paramremoteFile远程文件名,在上传之前已经将服务器工作目录做了改变*@paramlocalFile本地文件File句柄,绝对路径*@para...

望高手指点。拜谢!

/**

* 上传文件到服务器,新上传和断点续传

* @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变

* @param localFile 本地文件File句柄,绝对路径

* @param processStep 需要显示的处理进度步进值

* @param ftpClient FTPClient引用

* @return

* @throws IOException

*/

public uploadStatu uploadFile(String remoteFile,File localFile,FTPClient ftpClient,long remoteSize) throws IOException{

uploadStatu status;

//显示进度的上传

long step = localFile.length() / 100;

long process = 0;

long localreadbytes = 0L;

System.out.println("localFile本地文件绝对路径:"+localFile);

RandomAccessFile raf = new RandomAccessFile(localFile,"r");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaFTP 获取文件,你可以使用`FTPClient`类中的`retrieveFileStream`方法。以下是一个示例代码,演示如何获取文件: ```java import org.apache.commons.net.ftp.FTPClient; import java.io.*; public class FTPExample { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "your-username"; String password = "your-password"; String remoteFilePath = "/path/to/file.txt"; String localFilePath = "/path/to/save/file.txt"; FTPClient ftpClient = new FTPClient(); try { // 连接FTP服务器 ftpClient.connect(server, port); ftpClient.login(username, password); // 获取文件 InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath); // 将文件保存到本地文件 File file = new File(localFilePath); OutputStream outputStream = new FileOutputStream(file); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // 关闭 inputStream.close(); outputStream.close(); // 完成文件下载后,调用 completePendingCommand 方法 boolean success = ftpClient.completePendingCommand(); if (success) { System.out.println("File downloaded successfully."); } else { System.out.println("File download failed."); } } catch (IOException e) { e.printStackTrace(); } finally { try { // 断开连接 ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 在上述代码中,你需要替换 `server`、`port`、`username`、`password`、`remoteFilePath` 和 `localFilePath` 为你实际的 FTP 服务器和文件路径信息。然后使用 `ftpClient.retrieveFileStream(remoteFilePath)` 方法获取文件,将文件写入本地文件。 请注意,获取文件后,你还需要调用 `completePendingCommand` 方法来完成文件传输。这将告诉 FTP 服务器该文件已经接收完毕。 希望这可以帮助到你!如有更多问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值