问题
I have got a trouble while getting photo path from OpenShift server. I have got a Spring MVC app that have deployed at OpenShift server. There, every user could download there profile pics. I do it this way.
@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 = "";
String absoluteFilePath = "/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/";
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";
}
I'm putting my files to this path at OpenShift server
/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/
And after that i'm putting to database a path to this image that needed to get at user's page
this.usersService.addUserAvatar(principal.getName(),"/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/"+name);
When i'm opening user's page it shows me 404 error that GET https://appName-domain.rhcloud.com/haine/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/vr833vqI_wc.jpg
How to put a current path to my photos that it could have an access? Help pls!
回答1:
This how-to explains why it is difficult to serve user-uploaded files from a java application on openshift, and what code you can use to remedy the situation: https://forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets-on-openshift
来源:https://stackoverflow.com/questions/35186343/get-image-path-from-openshift-server-spring-mvc-jsp