将照片上传到OpenShift。 Spring MVC [英] Upload photos to OpenShift. Spring MVC
问题描述
我在OpenShift服务器上部署了我的Spring MVC应用程序的 .war 文件。
每个用户都可以更改照片。它适用于我的localhost,但我需要在OpenShift服务器上传照片并将每张照片的路径放到数据库中。
这是我上传文件的方法
I have deployed .war file of my Spring MVC app at OpenShift server. There every user can change there photos. It works at my localhost, but i need to upload photo at OpenShift server and to put a path of every photo to database. This is my method for uploading files
@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";
}
但这条路径不起作用。日志文件向我显示此异常
But this path doesn't work. Log file shows me this exception
/var/lib/openshift/56ae274f0c1e664bf3000158/jbossews/null/vr833vqI_wc.jpg
java.io.FileNotFoundException: null/vr833vqI_wc.jpg (No such file or directory)
求助!
推荐答案
解决了这个问题,添加< Context docBase =/ var / lib / openshift / PROJECT_ID / app-root / datapath =/ data/>
到 .openshift / config / server.xml 到 < HOST>
标记。
之后我可以通过 https://appname-domain.rhcloud.com/data/photo.jpg获取我的照片
Solved this problem adding <Context docBase="/var/lib/openshift/PROJECT_ID/app-root/data" path="/data" />
to the .openshift/config/server.xml into <HOST>
tag.
After that i could get my photos throught https://appname-domain.rhcloud.com/data/photo.jpg
这篇关于将照片上传到OpenShift。 Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!