问答 百科手机端

将照片上传到OpenShift 春季MVC

2023-05-25 19:22

解决了这个问题加入<Context docBase="/var/lib/openshift/PROJECT_ID/app-root/data" path="/data" /> 到 为<HOST>标签。在那之后我可以把我的照片https://img02.100why.cn/HCT_20221122/20221122photo.png

解决方法

我已经在OpenShift服务器上部署了Spring MVC应用程序的 .war
文件。每个用户都可以在那里更改照片。它在我的本地主机上工作,但是我需要在OpenShift服务器上上传照片,并将每张照片的路径放置到数据库中。这是我上传文件的方法

@RequestMapping(value = "/user/updateinfo",method = RequestMethod.POST)
public String postAvatar(@RequestParam("file") MultipartFile file,Principal principal) {
    if (file.isEmpty()) {
        return "redirect:/user";
    }

    if (!file.isEmpty()) {
        try {
            String relativeWebPath = "/resources/avatars/";
            String absoluteFilePath = context.getRealPath(relativeWebPath);
            String name = file.getOriginalFilename();
            // String name=file.getOriginalFilename();
            System.out.println(absoluteFilePath);
            String path = absoluteFilePath + "/" + name;
            File convFile = new File(absoluteFilePath + "/" + name);
            this.usersService.addUserAvatar(principal.getName(),"/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/"+name);
            System.out.println(convFile.getAbsolutePath());
            file.transferTo(convFile);
            System.out.println("You have uploaded file");
            return "redirect:/user";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Failed to upload");
            return "redirect:/user";
        }
    }
    return " redirect:/user";
}

但是这条路不起作用。日志文件显示了此异常

    /var/lib/openshift/56ae274f0c1e664bf3000158/jbossews/null/vr833vqI_wc.jpg
java.io.FileNotFoundException: null/vr833vqI_wc.jpg (No such file or directory)

请帮助!

总结

以上是真正的电脑专家为你收集整理的将照片上传到OpenShift。春季MVC的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。

热门