任何人都可以帮助我如何将图像上传到ftp服务器,而不是在春天存储到本地磁盘,我尝试上传但是它将图像存储在同一项目工作空间中,也存储在ftp服务器中。
我试过的代码?
@RequestMapping(method = RequestMethod.POST, value = "/updateprofile1", consumes = "multipart/form-data")
public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
FTPSClient con = getFTPConnection();
try {
con.setFileType(FTP.BINARY_FILE_TYPE);
// createDirectory(con, "test");
showServerReply(con);
System.out.println("Target directory: %s" + con.printWorkingDirectory());
con.setFileType(FTP.BINARY_FILE_TYPE);
con.enterLocalPassiveMode();
con.changeWorkingDirectory("user/user_profile");
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String fileName = currrentTime.getTimeInMillis() + "_" + file.getName()+".jpg";
boolean result = con.storeFile(fileName, file.getInputStream());
con.logout();
con.disconnect();
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
} catch (Exception e) {
redirectAttributes.addFlashAttribute("message", "Could not upload " + file.getOriginalFilename() + "!");
}
return "You successfully uploaded " + file.getOriginalFilename() + "!";
}
public static FTPSClient getFTPConnection() {
FTPSClient ftp = new FTPSClient();
int reply;
try {
ftp.connect("1***4");
ftp.login("****", "****);
reply = ftp.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
for (int i = 0; i < ftp.listDirectories().length; i++) {
System.out.println(ftp.listDirectories());
}
ftp.changeWorkingDirectory("user");
showServerReply(ftp);
// LOGGER.info("-------->>> %s", ftp.printWorkingDirectory());
showServerReply(ftp);
//LOGGER.info("Connected Success");
} else {
//LOGGER.error("Connection Failed");
ftp.disconnect();
ftp = null;
}
} catch (SocketException ex) {
//LOGGER.error("Error while trying to connect to the ImageBak Repository");
ex.printStackTrace();
ftp = null;
} catch (IOException ex) {
//LOGGER.error("Error while trying to connect to the ImageBak Repository");
ex.printStackTrace();
ftp = null;
}
return ftp;
}
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER: " + aReply);
}
}
}
答案 0 :(得分:2)
尝试使用this示例,将Apache Commons Net添加到您的项目中:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
然后更新FileUploadController#handleFileUpload
这样的内容:
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
String FTP_ADDRESS = "your_address";
String LOGIN = "you_login";
String PSW = "you_password";
FTPClient con = null;
try {
con = new FTPClient();
con.connect(FTP_ADDRESS);
if (con.login(LOGIN, PSW)) {
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
boolean result = con.storeFile(file.getOriginalFilename(), file.getInputStream());
con.logout();
con.disconnect();
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
}
} catch (Exception e) {
redirectAttributes.addFlashAttribute("message",
"Could not upload " + file.getOriginalFilename() + "!");
}
return "redirect:/";
}