FTPClient - Java, upload file(FTPClient - Java,上传文件)
问题描述
我正在尝试进行非常简单的文件上传.我想要一个可以上传我告诉它的任何文件的 Java FTPClient.但是 pdf 总是一团糟,我的 pdf 编辑器 (Adobe) 不会打开它,说存在 I/O 错误.
I'm trying to do a VERY simple file upload. I want a Java FTPClient that can upload any file I tell it to. But the pdf always gets all messed up and my pdf editor (Adobe) won't open it, saying there is an I/O error.
我正在使用以下类:
import org.apache.commons.net.ftp.FTPClient;
....
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("mydomain.com");
client.login("user", "password");
String filename = "myPDF.pdf";
fis = new FileInputStream(filename);
client.storeFile("temp.pdf", fis);
fis.close();
client.logout();
} catch (IOException e) {
e.printStackTrace();
}
为什么这不起作用,我该如何解决?
Why doesn't this work, and how do I fix it?
推荐答案
因为 FTPClient 是 FTP.ASCII_FILE_TYPE.您只需要更新配置即可以二进制模式传输.
It doesn't work because the default transfer mode for FTPClient is FTP.ASCII_FILE_TYPE. You just need to update the configuration to transfer in binary mode.
这篇关于FTPClient - Java,上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FTPClient - Java,上传文件
基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
