java - 无法将UploadedFile上传到FTP服务器?

标签 java jsf upload ftp

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

上传文件到FTPServer的 Controller 方法

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 服务器
并查看文件名,但其大小 = 零!!
然后几秒钟后这个文件就会消失 enter image description here

最佳答案

问题原因
本地计算机中的防火墙正在运行

这就是为什么?
因为在开发环境中
我的机器被模拟为网络服务器(因为它运行本地服务器)
我的应用程序需要将文件写入FTP服务器
因此需要获得与(FTP服务器)通信的权限
防火墙仅允许与确定的(服务器:端口)连接
默认情况下,防火墙会阻止 FTP 连接 (AnyServer:20/AnyServer:21)

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

关于java - 无法将UploadedFile上传到FTP服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19527356/

相关文章:

java - 使用带位图的 Universal ImageLoader 显示图像

java - Netbeans 找不到 java.io.InputStream

java 。比较字符串时忽略重音符号

java - Slick2d 使用动画/图像抗锯齿

java - 简单的上传速度测试

html - 用户如何<h :messages> only if not null messages?

java - 标识 'chartBean' 未引用 MethodExpression 实例

java - 不要在 ajax 上重置小程序

java - 将多部分表单数据上传到 php

java - 使用文件协议(protocol)的 Jmeter HTTP 请求未获取所有文件