如何使用spring MultipartFile将图像上载到ftp服务器,而不将图像存储在本地磁盘中

如何使用spring MultipartFile将图像上载到ftp服务器,而不将图像存储在本地磁盘中,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,有谁能帮我在春季将图像上传到ftp服务器而不存储到本地磁盘吗?我尝试了上传,但它将图像存储在同一个项目工作空间中,也存储在ftp服务器中 下面是我试过的代码 @RequestMapping(method = RequestMethod.POST, value = "/updateprofile1", consumes = "multipart/form-data") public String handleFileUpload(@RequestParam("file") MultipartFil

有谁能帮我在春季将图像上传到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);
        }
    }
}
@RequestMapping(method=RequestMethod.POST,value=“/updateprofile1”,consumes=“multipart/formdata”)
公共字符串handleFileUpload(@RequestParam(“文件”)多部分文件文件,重定向属性重定向属性){
FTPSClient con=getFTPConnection();
试一试{
con.setFileType(FTP.BINARY文件类型);
//createDirectory(con,“test”);
showServerReply(con);
System.out.println(“目标目录:%s”+con.printWorkingDirectory());
con.setFileType(FTP.BINARY文件类型);
con.enterLocalPassiveMode();
con.changeWorkingDirectory(“用户/用户配置文件”);
con.enterLocalPassiveMode();//重要!
con.setFileType(FTP.BINARY文件类型);
字符串文件名=currentTime.getTimeInMillis()+““+file.getName()+”.jpg”;
布尔结果=con.storeFile(文件名,file.getInputStream());
con.logout();
con.disconnect();
redirectAttributes.addFlashAttribute(“消息”,
“您已成功上载”+文件.getOriginalFilename()+“!”;
}捕获(例外e){
redirectAttributes.addFlashAttribute(“消息”,“无法上载”+文件.getOriginalFilename()+“!”);
}
返回“您已成功上载”+file.getOriginalFilename()+“!”;
}
公共静态FTPSClient getFTPConnection(){
FTPSClient ftp=新的FTPSClient();
int回复;
试一试{
ftp.connect(“1***4”);
ftp.login(“****”,“***”);
reply=ftp.getReplyCode();
如果(FTPReply.isPositiveCompletion(回复)){
对于(int i=0;i>>%s”,ftp.printWorkingDirectory());
showServerReply(ftp);
//LOGGER.info(“连接成功”);
}否则{
//LOGGER.error(“连接失败”);
ftp.disconnect();
ftp=null;
}
}捕获(SocketException例外){
//LOGGER.error(“尝试连接到ImageBak存储库时出错”);
例如printStackTrace();
ftp=null;
}捕获(IOEX异常){
//LOGGER.error(“尝试连接到ImageBak存储库时出错”);
例如printStackTrace();
ftp=null;
}
返回ftp;
}
私有静态void showServerReply(FTPClient FTPClient){
字符串[]答复=ftpClient.getReplyStrings();
如果(回复!=null&&replements.length>0){
for(字符串aReply:答复){
System.out.println(“服务器:+aReply”);
}
}
}
尝试使用示例,将Apache Commons Net添加到您的项目中:

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>

到目前为止,您尝试了什么?请向我们展示您的代码。下面是我尝试的代码?请编辑问题以添加代码,而不是将其作为此问题的答案添加。这将更易于阅读。在问题下方有共享//标志链接。它可以工作,但我上载了图像,但以文件格式存储。请查看我尝试过的上述代码,但它是sto文件格式我已经编辑了我的答案-在
con.storeFile
中将
file.getName()
更改为
file.getOriginalFilename()
。如果答案对您有帮助,请不要忘了访问/upvote。谢谢,但我已将file.getName更改为multipartFile.getName()+“.jpg”;它正在工作
@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:/";
}