使用Apache Commons文件上传

时间:2013-10-27 01:15:26

标签: java apache file-upload ftp

我正在尝试将pdf文件上传到ftp服务器。我的代码有点像这样:

public void pdfUpload(String ticket, JLabel message) {     

FTPClient client = new FTPClient(); 
        FileInputStream fis = null;
        try {
            client.connect("www.mydomain.com", 21);
            client.login("user", "userpass");


            client.setFileType(FTP.BINARY_FILE_TYPE); // optional


            String FileName = ticket.replace("/", "_");
            File fil = new File("pdf\\"+FileName+".pdf");
            message.setText(FileName+".pdf is being uploaded... Please wait" );
            fis = new FileInputStream(fil);

            String remoteFile = fil.getName();
            client.storeFile(remoteFile, fis);
            client.logout();
            message.setText("File Uploaded sccessfully");
        } 

        catch (IOException e) {
            message.setText("Failed to upload pdf file"+e);
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
            message.setText("Failed to upload pdf file");
            }
        }    
}

该方法显示文件已上传,该方法执行正常。并显示完成消息。但我在ftp中找不到该文件。这意味着文件未上传。我的代码有什么问题。请帮忙。

2 个答案:

答案 0 :(得分:0)

登录后尝试调用enterLocalPassiveMode。

可能是很多东西,因为FTP服务器是不可预知的小怪物,但这就是它对我来说的味道。

答案 1 :(得分:0)

看起来您的文件路径在远程系统上作为文件名无效,或者根据文件路径名格式未按预期存储它。保存到远程系统时,请尝试仅使用不带“pdf \\”前缀的文件名。