将照片上传到OpenShift春季MVC

【字号: 日期:2024-03-25浏览:51作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决将照片上传到OpenShift春季MVC?

解决了这个问题加入<Context docBase='/var/lib/openshift/PROJECT_ID/app-root/data'path='/data' /> 到 为<HOST>标签。在那之后我可以把我的照片https://appname-domain.rhcloud.com/data/photo.jpg

解决方法

我已经在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.jpgjava.io.FileNotFoundException: null/vr833vqI_wc.jpg (No such file or directory)

请帮助!

相关文章: