我想知道确实'put'是否已成功将文件放到目标位置。如果由于任何原因文件未放入目的地[可能由于目标服务器中的问题,如空间限制等],我需要知道。
代码:
private static boolean putFile(String m_sLocalFile, FtpClient m_client) {
boolean success = false;
int BUFFER_SIZE = 10240;
if (m_sLocalFile.length() == 0) {
System.out.println("Please enter file name");
}
byte[] buffer = new byte[BUFFER_SIZE];
try {
File f = new File(m_sLocalFile);
int size = (int) f.length();
System.out.println("File " + m_sLocalFile + ": " + size + " bytes");
System.out.println(size);
FileInputStream in = new FileInputStream(m_sLocalFile);
OutputStream out = m_client.put(f.getName());
int counter = 0;
while (true) {
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
System.out.println(counter);
}
out.close();
in.close();
} catch (Exception ex) {
System.out.println("Error: " + ex.toString());
}
return success;
}
答案 0 :(得分:0)
我希望它会抛出IOException
。你有理由相信它没有吗?但是您不应该直接使用该类,在调用URL
之后,您应该使用ftp:URLConnection
及其setDoOutput(true)
类来执行I / O.