当我尝试将 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 服务器
并查看文件名,但其大小 = 零!!
然后几秒钟后这个文件就会消失
最佳答案
问题原因
本地计算机中的防火墙
正在运行
这就是为什么?
因为在开发环境中
我的机器被模拟为网络服务器
(因为它运行本地服务器)
我的应用程序
需要将文件写入FTP服务器
因此需要获得与(FTP服务器)通信的权限
防火墙仅允许与确定的(服务器:端口)连接
默认情况下,防火墙会阻止 FTP 连接 (AnyServer:20/AnyServer:21)
解决方案
所以当我禁用防火墙时
问题已解决
关于java - 无法将UploadedFile上传到FTP服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19527356/