gpt4 book ai didi

Java通过ftp上传文件零字节错误

转载 作者:行者123 更新时间:2023-11-30 04:59:55 25 4
gpt4 key购买 nike

我将文件上传到服务器,但文件为空。(大小为零字节)

     int reply;
ftp.connect(server,215);
ftp.login(username, Password);
System.out.println("Connected to " + server + ".");
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
}
System.out.println("FTP server connected.");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();

InputStream input= new FileInputStream(source_file_path);

ftp.storeFile(dest_dir, input);
System.out.println(ftp.storeFile(dest_dir, input));
System.out.println( ftp.getReplyString() );

input.close();

ftp.logout();

最佳答案

查看您的代码,我认为您正在使用 Jakarta commons net。如果这是真的,请尝试以下操作:

ftp.connect(address, port);
boolean ft = ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
if(!ft)
throw new Exception("Error");
ftp.enterLocalPassiveMode(); or ftp.enterLocalActiveMode();
boolean log = ftp.login(user, password);
//if log == true, then u are logged in
ftp.storeFile(remote, local);

此外,如果您想使用进度条来获取传输进度,您可以尝试此方法而不是 ftp.storeFile

InputStream stO = new BufferedInputStream(new FileInputStream(file), ftp.getBufferSize());
OutputStream stD = ftp.storeFileStream(file.getName());

org.apache.commons.net.io.Util.copyStream(
stO,
stD,
ftp.getBufferSize(),
file.length(),
new org.apache.commons.net.io.CopyStreamAdapter()
{
@Override
public void bytesTransferred(long totalBytesTransferred, int bytesTransferred,
long streamSize)
{
try
{
pb.setMaximum((int)streamSize);
pb.setValue((int)totalBytesTransferred);
}
catch(Exception ex)
{
pb.setMaximum(-1);
}
}
});
stO.close();
stD.close();
boolean ok = ftp.completePendingCommand();
if(!ok)
throw new Exception("ERROR while sending the file");

PS:pb是一个JProgressBar

关于Java通过ftp上传文件零字节错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7148425/

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