无法将UploadedFile上传到FTP服务器? [英] Unable to upload UploadedFile to FTP Server?

查看:197
本文介绍了无法将UploadedFile上传到FTP服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试上传 UploadedFile 对象内容到FTP服务器
[它的值从[t:inputFileUpload]组件获取]

在我的Web浏览器中的加载指示器播放并不会停止

当调试时,我发现我的来临冻结在一行

ftp.storeFile (destFilePath,inputStream);
然而,我尝试3种方法(代码中显示)上传

,但仍然所有方法都无法正常工作



UploadedFile到FTPServer的控制器方法

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

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

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

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

//方法2
// InputStream inputStream = uploadedFile.getInputStream();

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

destFilePath = uploadedFile.getName();

// ftp服务器
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();
}
}
返回destFilePath;
}

注意

当Freeze检查我的FTP服务器

然后看到文件名BUT其大小=零!!

然后几秒钟后这个文件将消失

解决方案

/ strong>

防火墙在本地机器上运行



那是为什么?

因为在开发环境中
我的机器被模拟为一个web服务器(因为它运行本地服务器)

我的应用程序需要将文件写入 FTP服务器

因此需要获得与(FTP服务器)通信的权限

防火墙只允许与确定的(服务器:端口)连接
并且默认情况下,防火墙阻止FTP连接(AnyServer :20 / AnyServer:21)

解决方案

因此,当我禁用防火墙

问题解决


When i try to upload UploadedFile object content To FTP server
[That its value get from [t:inputFileUpload] component]
The loading indicator in my web browser play and will not stop
When debug i found that my come freezes in a line
ftp.storeFile(destFilePath, inputStream); However i try 3 methods (shown in code) to upload
BUT still all methods not working

Controller method that UploadedFile to 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;
}

Note
When Freeze i check the my FTP server
And see the file name BUT its size = zero !!
Then after some seconds this file will disappeared

解决方案

The Problem Reason
Firewall in local machine is running

Thats Why ?
Because In development environment
My machine is simulated as a web server (Because thats run local server)
And my application need to write a file to a FTP server
So need to take a privilege to communicate with (FTP server)
The firewall ONLY allow connections with determined (Servers:Ports)
And By default firewall prevent FTP connections (AnyServer:20 / AnyServer:21)

Solution
So when i disabled Firewall
The problem resolved

这篇关于无法将UploadedFile上传到FTP服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆