gpt4 book ai didi

Java:上传文件到FTP问题(数据包丢失)

转载 作者:行者123 更新时间:2023-12-03 23:07:07 25 4
gpt4 key购买 nike

我正在尝试将文件从我的 Java 应用程序传输到 FTP 服务器程序可以运行,文件已传输,但是当我在 FTO 文件夹中打开时,文件已损坏,我认为数据包在文件传输过程中丢失了。为什么?我该如何解决这个问题?

另一个问题,如果我想停止文件上传,如何停止while

谢谢大家!

我类里的代码:

FTPClient client = new FTPClient();
InputStream is = null;
//...
try{
client.connect(MY_FTP_URL);
client.login(USER, PASS);
InputStream is = new FileInputStream(file_path);
OutputStream os = client.storeFileStream(file_name);
byte[] buffer = new byte[1024];
int len;
//I use this way to check the transfer progress
while((len = is.read(buffer)) != -1){
os.write(buffer, 0, len);
os.flush();
}
os.close();
} catch (IOException e){
e.printStackTrace();
} finally{
try{
if(is != null){
is.close();
}
client.disconnect();
} catch(IOException e){
e.printStackTrace();
}
}

最佳答案

查看 FAQ :

Q: Why are my files corrupt after transfer?

A: The most common cause for this is when the file is transfered as ASCII but the contents of the file are not ASCII and the file should be transferred as BINARY. RFC 959 says the default transfer mode should be ASCII. FTPClient conforms to the standard. You must explicitly call setFileType(FTP.BINARY_FILE_TYPE); to request binary transfer mode after logging in to the FTP server.

调用 setFileType(FTP.BINARY_FILE_TYPE);

关于Java:上传文件到FTP问题(数据包丢失),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4731214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com