我试图将文件从我的Java应用程序传输到FTP服务器 程序工作,文件传输,但是当我走在FTO文件夹打开,该文件已损坏,我认为数据包在文件传输过程中丢失。为什么?我该如何解决这个问题?的Java:上传文件到FTP问题(数据包丢失)
另一个问题,我怎么能停止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();
}
}
(1)不写这个自己,使用图书馆(2)不使用FTP,除非你绝对必须使用[SCP via JSch](http://seancode.blogspot.com/2008/02/jsch-scp-file-in-java.html)。 – 2011-01-19 02:32:25
不知道你的问题是什么,但它几乎肯定不是“数据包丢失” - FTP分层在TCP之上,它提供了一个可靠的字节流。 – 2011-01-19 02:42:31