2016-12-09 17 views
0

私のSpring Controllerでは、リソースフォルダからJSPページにイメージを返すメソッドを書きました。しかし、リソースを変更した場合(または)新しいInputStreamがnullの場合アプリをリロードすると、再び動作します。アプリケーションをリロードせずにSpringコントローラでアップロードした後にイメージを取得する方法は?

私は、アプリケーションをリロードせずに画像を取得するために何ができますか?

コントローラの私の方法:JSPページで

@ResponseBody 
    @RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) 
    public byte[] testphoto(HttpServletRequest request, 
          @PathVariable("authorUsername") String authorUsername, 
          @PathVariable("title") String title) throws IOException { 
     InputStream in = servletContext.getResourceAsStream("/resources/uploadImages/zeus192/" + title + "/0.jpg"); 
     return IOUtils.toByteArray(in); 
    } 

<img src="/image/${advert.authorUsername}/${advert.title}/"> 

EDITED:

@ResponseBody 
    @RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) 
    public void testphoto(HttpServletRequest request, HttpServletResponse response, 
             @PathVariable("authorUsername") String authorUsername, 
             @PathVariable("title") String title) throws IOException { 

     InputStream instream = servletContext.getResourceAsStream("/resources/uploadImages/zeus192/" + title + "/0.jpg"); 
     byte[] bytes = IOUtils.toByteArray(instream); 
     int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream()); 
     response.setContentLength(contentLength); 
     response.setHeader("Content-Disposition", "inline;filename=\"" + "0.jpg" + "\""); 
    } 

EDITED 2:

@ResponseBody 
@RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) 
public void testphoto(HttpServletRequest request, HttpServletResponse response, 
            @PathVariable("authorUsername") String authorUsername, 
            @PathVariable("title") String title) throws IOException { 

    String contextPath = request.getSession().getServletContext().getRealPath(""); 
    String directory = contextPath.substring(0,contextPath.length() - 18) + "/target/YaPokupay/src/main/webapp/resources/uploadImages/" + authorUsername + "/" + title + "/0.jpg"; 

    File file = new File(directory); 
    String filePath = file.getPath(); // D:\Works\YaPokupay\target\YaPokupay\src\main\webapp\resources\uploadImages\zeus192\Картинка\0.jpg 
    InputStream instream = this.getClass().getClassLoader().getResourceAsStream(filePath); 

    byte[] bytes = IOUtils.toByteArray(instream); 
    int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream()); 
    response.setContentLength(contentLength); 

    response.setHeader("Content-Disposition", "inline;filename=\"" + "0.jpg" + "\""); 

} 

答えて

1

これは、同じresource.Myリソースのみとめ要求のために私の作品(../ProjectName/src/main/webapp/resources/images/logos)である

@Autowired 
private ServletContext servletContext; 

@RequestMapping(value = "/sampleJavaOperation", method = RequestMethod.GET) 
    public void sampleJavaOperation(HttpServletRequest request, HttpServletResponse response) throws IOException { 

     InputStream instream = servletContext.getResourceAsStream("/resources/images/logos/wavemaker_62x62.png"); 
     byte[] bytes = IOUtils.toByteArray(instream); 
     int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream()); 
     response.setContentLength(contentLength); 
     response.setHeader("Content-Disposition", "inline;filename=\"" + "0.png" + "\""); 
    } 
+0

_ @ WMAccessVisibility(value = AccessSpecifier.APP_ONLY)_と_ @ ApiOperation(value = "")_はどういう意味ですか? – Dmitry

+0

sry、これらは自分の注釈です。更新されたコードで、このサービスには影響しません。 –

+0

このコードは、ターゲットフォルダから読み込まれた画像と同じように動作します。 – Dmitry

1

ここでは、パラメータとして春controller.AddingのHttpServletResponseからファイルをダウンロードし、応答出力ストリームにファイルストリームを設定するためのコードです。

@ResponseBody 
    @RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) 
    public void testphoto(HttpServletRequest request,HttpServletResponse response, 
          @PathVariable("authorUsername") String authorUsername, 
          @PathVariable("title") String title) throws IOException { 
     InputStream in = servletContext.getResourceAsStream("/resources/uploadImages/zeus192/" + title + "/0.jpg"); 
     Byte[] bytes = IOUtils.toByteArray(in); 
     int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream()); 
     httpServletResponse.setContentLength(contentLength); 
    } 

ブラウザで画像をレンダリングする場合は、ローカルマシンにファイルをダウンロードしますインライン他の添付ファイルとして、ヘッダの「Content-処分」を追加します。

if(inline){ // if you want to attachment to be inline 
    response.setHeader("Content-Disposition", "inline;filename=\"" + fileName + "\""); 
}else{ 
    response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); 
} 
+0

私が作成する必要があります新しい応答?または応答とhttpServletResponseは同じものですか? – Dmitry

+0

ここで「Content-Disposition」を追加する必要がありますか? – Dmitry

+0

パラメータで宣言されているのと同じHttpServletResponse paramを使用して、Content-Dispositionヘッダーを設定します。httpサーブレット応答で更新されたコード。 –

関連する問題