2016-02-03 72 views
1

從OpenShift服務器獲取照片路徑時遇到了麻煩。 我有一個在OpenShift服務器上部署的Spring MVC應用程序。那裏,每個用戶都可以在那裏下載配置文件圖片。我這樣做。從OpenShift服務器獲取圖像路徑。 Spring MVC。 JSP

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

我把我的文件,這個路徑在OpenShift服務器

/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/ 

而在這之後我把數據庫的路徑,以這個形象,需要得到在用戶的頁面

this.usersService.addUserAvatar(principal.getName(),"/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/"+name); 

,當我打開用戶的頁面它顯示了我404錯誤GET https://appName-domain.rhcloud.com/haine/var/lib/openshift/56ae274f0c1e664bf3000158/app-root/data/vr833vqI_wc.jpg

^h是否將當前路徑放入我的照片中,以便可以訪問?請幫助!

回答

0

這如何做解釋了爲什麼它是很難從openshift Java應用程序服務於用戶上傳的文件,什麼代碼,你可以用它來糾正這種情況:https://forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets-on-openshift

+0

,如果你正在談論https://開頭blog.openshift.com/multipart-forms-and-file-uploads-with-tomcat-7/這個話題,有不正確的答案! 原因在那裏教程說,我需要更新** context.xml **' '在這條線上!它不起作用,因爲這個文件不與服務器連接!正確的答案是在** .openshift/config/server.xml中更新''** –