【问题标题】:Unable to upload UploadedFile to FTP Server?无法将 UploadedFile 上传到 FTP 服务器?
【发布时间】:2013-10-22 20:09:40
【问题描述】:

当我尝试将 UploadedFile 对象内容上传到 FTP 服务器时
[它的值来自 [t:inputFileUpload] 组件]
我的网络浏览器中的加载指示器播放并且不会停止
调试时我发现我的来了冻结在一行
ftp.storeFile(destFilePath, inputStream); 但是我尝试了 3 种方法(如代码所示)上传
但仍然所有方法都不起作用

UploadedFile 到 FTPServer 的控制器方法

public static String uploadFileToFTPServer
    (String ftpServerIP, String userName, String password,UploadedFile uploadedFile) {

    String destFilePath = null;
    FTPClient ftp = new FTPClient();

    ftp.addProtocolCommandListener(new PrintCommandListener(
            new PrintWriter(System.out)));
    try {

        // Method 1
        byte[] fileBytesContent = uploadedFile.getBytes();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytesContent);

        // Method 2
//          InputStream inputStream  = uploadedFile.getInputStream();

        // Method 3
//          InputStream inputStreamContent  = uploadedFile.getInputStream();
//          BufferedInputStream inputStream = new BufferedInputStream(inputStreamContent);

        destFilePath = uploadedFile.getName() ;

        // ftp server
        ftp.connect(ftpServerIP);
        ftp.login(userName, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        ftp.storeFile(destFilePath, inputStream);

        inputStream.close();
        ftp.logout();

    } catch (Exception e) {
        destFilePath = null;             
        e.printStackTrace();

    } finally {
        try {
            ftp.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return destFilePath;
}

注意
冻结时,我检查我的 FTP 服务器
并查看文件名,但其大小 = 零!!
然后几秒钟后这个文件就会消失

【问题讨论】:

    标签: java jsf upload ftp


    【解决方案1】:

    问题原因
    本地机器中的Firewall正在运行

    那是为什么?
    因为在开发环境中
    我的机器模拟为a web server(因为那是运行本地服务器)
    my application需要写一个文件到a FTP server
    所以需要获取特权才能与(FTP服务器)通信
    防火墙只允许与确定的(服务器:端口)的连接
    并且默认情况下防火墙会阻止 FTP 连接 (AnyServer:20 / AnyServer:21)

    解决方案
    所以当我禁用防火墙时
    问题解决了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多