如何在sun.net.ftp.FtpClient中捕获ftp成功/失败?

时间:2012-04-05 08:04:03

标签: java ftp put ftp-client

我想知道确实'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;
}

1 个答案:

答案 0 :(得分:0)

我希望它会抛出IOException。你有理由相信它没有吗?但是您不应该直接使用该类,在调用URL之后,您应该使用ftp:URLConnection及其setDoOutput(true)类来执行I / O.