问题描述
我的Spring Boot应用程序有问题。 当我将文件上传到ftp服务器时,文件会以某种方式更改其格式。 我再也无法打开图片了。
我通过百里香上传。
在Controller中,我通过以下方法使用上载服务:
public void fileUpload(multipartfile file) {
try {
FtpClient ftp = new FtpClient(ftpHost,ftpPort,ftpUser,ftpPassword);
ftp.open();
String tmpdir = System.getProperty("java.io.tmpdir");
File physicalFile = new File(tmpdir+file.getoriginalFilename());
file.transferTo(physicalFile);
ftp.putFiletoPath(physicalFile,file.getoriginalFilename());
ftp.close();
} catch (IOException e) {
e.printstacktrace();
}
}
这是我的FtpClient方法:
public void open() throws IOException {
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftp.connect(server,port);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new IOException("Exception in connecting to FTP Server");
}
ftp.login(user,password);
}
public void close() throws IOException {
ftp.disconnect();
}
public void putFiletoPath(File file,String path) throws IOException {
ftp.storeFile(path,new FileInputStream(file));
}
一切正常-文件出现在服务器上,甚至具有相同的重量。
...但是我无法打开。
我尝试用来打开它的任何图形程序都说这是错误的文件格式...
有什么想法吗?
解决方法
在我的FtpClient类中-我添加了一行
在void open()方法中:
public void open() throws IOException {
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftp.connect(server,port);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new IOException("Exception in connecting to FTP Server");
}
ftp.login(user,password);
// IT WAS THE PROBLEM - I didn't have it:
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
感谢帮助