Unable to upload UploadedFile to FTP Server?

2019-09-06 01:05发布

问题:

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

回答1:

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