使用FTP客户端上传文件

时间:2018-09-24 11:04:44

标签: java ftp ftp-client apache-commons-net

我正在尝试将文件从本地系统上传到ftp服务器的远程路径。 ftpClient.storeFile 始终返回false。我无法识别该错误。预先感谢。

public boolean upload(String srcPath, String destPath, String filename) {
    InputStream inputStream = null;
    try {
        // connect(): basically creates and initialises the ftpClient object
        connect();
        File localFile      = new File(srcPath + filename);
        String remotePath   = destPath + filename;
        inputStream         = new FileInputStream(localFile);
        if(ftpClient.storeFile(remotePath, inputStream)) {
            System.out.println("File uploaded successfully!!");
            return true;
        } else
            System.out.println("Upload failed!!");
    } catch (IOException ioExp) {
        ioExp.printStackTrace();
    } finally {
        if(inputStream != null)
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        disconnect();
    }
    return false;
}

0 个答案:

没有答案