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

发布于 2025-01-10 22:05:17 字数 1830 浏览 0 评论 0原文

您好,我是 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 实现它。

谢谢

Hi I am new to Springboot I was trying to develop a application, One of its functionality is to upload profile Image. It was working fine in STS but when I pack it in jar and hosting it on AWS EC2 envirnment I am getting some error while processing that image

Error:

enter image description here

handler for profile picture:

@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";
    }

It is working fine in IDE after some research I found way of writing data in jar is diffrent could some please help me how can I implemenr it for jar also.

Thankyou

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2025-01-17 22:05:17

您需要做的就是将此行替换

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

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

:这将像魅力一样发挥作用。

all you need to do is replace this line:

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

With:

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

THat will work like charm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文