作为 jar 托管时,更新配置文件图像功能不起作用

update profile image functionality is not working while hosting as jar

嗨,我是 Springboot 的新手,我正在尝试开发一个应用程序,其功能之一是上传配置文件图像。它在 STS 中运行良好,但是当我将它打包到 jar 中并将其托管在 AWS EC2 环境中时,我在处理该图像时遇到了一些错误

错误:

个人资料图片处理程序:

@PostMapping("/process-contact")
    public String processContact(@ModelAttribute Contact contact, @RequestParam("profileImage") MultipartFile file,
            HttpSession session) {

        try {

            contact.setUser(user);
            user.getContacts().add(contact);
            // processing and uploading photo

            if (file.isEmpty()) {
                System.out.println("File is empty");
                contact.setImage("contact.png");

            } else {
                
                //Processing Image
                InputStream inputStream = file.getInputStream();
                Path paths = Paths.get(new ClassPathResource("/static/img").getFile().getPath()+"/" +file.getOriginalFilename());
                
                Files.copy(inputStream, paths, StandardCopyOption.REPLACE_EXISTING);
                contact.setImage(file.getOriginalFilename());
                
            }
            
            // Success Message
            session.setAttribute("message", new Message("Your contact is added...", "success"));
            
            this.userRepository.save(user);
            System.out.println("Successfully Added");
        } catch (Exception E) {
            E.printStackTrace();

            // Failed message
            session.setAttribute("message", new Message("Something went wrong "+E.getMessage(), "danger"));
        }

        return "normal/add_contact_form";
    }

它在 IDE 中工作正常经过一些研究我发现在 jar 中写入数据的方式不同,请帮助我如何也为 jar 实现它。

谢谢

您需要做的就是替换这一行:

Path paths = Paths.get(new ClassPathResource("/static/img").getFile().getPath()+"/" +file.getOriginalFilename());

与:

Path paths = Paths.get(new FileSystemResource("/static/img").getFile().getPath()+"/" +file.getOriginalFilename());

这会很有魅力。