2016-01-18 79 views
0

將公共圖像存儲在文件系統上的一般方法是什麼? 我有一個endpoint在服務器上保存文件。但我不明白的是如何使它公開可訪問,以便我不必爲這些公共文件創建端點(我認爲這將是不必要的開銷)?我是否需要添加一些單獨的文件服務器? 我的web應用程序的架構是:春季將上傳文件保存爲公共資源

Angular.js client 
    | 
Node.js client server 
    | 
Spring backend on separate server 

我想實現的是類似

<img src="localhost:8000/public/images/myImage"/> 

我來到this答案,但正在端點真正正確的方式去?

回答

0

您可以將圖像保存到任何路徑,但不需要webroot。您可以在顯示它們的同時從該目錄中讀取圖像。

ImageController.java

package com.expertwebindia; 

    import java.io.File; 
    import java.io.FileInputStream; 

    import javax.servlet.http.HttpServletResponse; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.bind.annotation.RequestParam; 
    import org.springframework.web.bind.annotation.ResponseBody; 

    @Controller 
    public class ImageController { 
     private String path = "E:/uploads/"; 
     @RequestMapping(value = "/image/display",method=RequestMethod.GET) 
     @ResponseBody 
     public byte[] helloWorld(@RequestParam String name,HttpServletResponse response) { 
      System.out.println("Show is invoked"); 
      response.setContentType("image/jpeg"); 
      File file; 
      byte arr[]={}; 
      try{ 
       file = new File(path+name); 
        if(file.isFile()){ 
         System.out.println("File is found"); 
        } 
        else{ 
         name = "no-image.jpg"; 
         file = new File(path+name); 
        } 
       arr= new byte[(int)file.length()]; 
       FileInputStream fis = new FileInputStream(file); 
       fis.read(arr,0,arr.length); 
      }catch(Exception e){ 
       System.out.println(e); 
      } 
      return arr; 
     } 
    } 

在JSP哪裏需要顯示需要投入<img>標籤具有以下。注意我在這裏使用了JSTL標籤,因此您需要將JSTL jar添加到WEB-INF/lib並將taglib添加到JSP頁面。

<img alt="" src="<c:url value="/image/display?name=${image_name}"/>"/> 

Learn more about Spring MVC file upload

0

如果你想存儲在您的應用程序資源文件夾的圖像,試試這個:

A.資源映射(這個文件夾內的所有目錄將可以訪問):

<mvc:resources location="/resources/" mapping="/resources/**"/> 

B.將此加入到jsp頁面中:

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 

<img src="<spring:url value='/resources/profile-pictures/${user.profileImage}'/>" class="img-circle" alt="User Image"> 

注意:所有上傳的圖像將被存儲在src\main\webapp\resources\profile-pictures目錄下。

C.禁用安全:

<http pattern="/resources/**" security="none"/> 

D.方法來保存圖片:

@RequestMapping(value="/upload", method = RequestMethod.POST) 
public String handleFormUpload(@RequestParam("imageFile") MultipartFile file, Principal principal) { 
    String webappRoot = servletContext.getRealPath("/"); 
    String LoggedInUser = principal.getName(); 
    User user = userService.findLoggedInUser(LoggedInUser); 
    //System.out.println(user.toString()); 
    try { 
     if (!file.isEmpty()) { 
      BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes())); 
      File destination = new File(webappRoot + "/resources/profile-pictures/ProfileImage"+user.getName()+".jpg"); // something like C:/Users/tom/Documents/nameBasedOnSomeId.png 
      ImageIO.write(src, "png", destination); 
      System.out.println("users profile Pic is stored at "+user.getProfileImage() + "UserPassword :" +user.getPassword()); 
      user.setProfileImage("ProfileImage"+user.getName()+".jpg"); 
      userService.update(user); 
      System.out.println("Image is stored at "+ user.getProfileImage()); 
     } 
    } catch (Exception e) { 
     System.out.println("Exception occured" + e.getMessage()); 
     return "redirect:/account/upload?success=false"; 
    } 
    return "redirect:/account/upload?success=true"; 
} 

PS:這不是理想的解決方案,而是適用於學習的目的。我也在尋找免費的圖像存儲。我遇到了Amazon S3,但它需要信用卡,然後是Box,Dropbox。但我發現這個Cloudinary很有趣。

PPS:您可以看到我的演示應用程序here