2017-09-01 33 views
0

我想發送一個文件作爲REST請求的響應。 我找到了一個解決方案來完成這個工作。 (我發現堆棧溢出的代碼,但是,對不起,我找不到鏈接,我在下面報告它。)以Spring REST作爲包發送文件

我的問題是這個進程將發送文件作爲包?所以如果接收緩慢的互聯網連接會收到一個超時(因爲文件一次發送),或者這是處理?

謝謝你的幫助。

代碼:

@RequestMapping(value = "/download", method = RequestMethod.GET) 
public ResponseEntity<?> downloadCompressed(@RequestParam("filename") String filename, HttpServletRequest request) { 

    ResponseEntity respEntity = null; 
    byte[] reportBytes = null; 

    File result = new File(Configuration.OUTPUTDIR.getValue() + filename); 
    try { 
     if (result.exists()) { 
      InputStream inputStream = new FileInputStream(Configuration.OUTPUTDIR.getValue() + filename); 
      String type = URLConnection.guessContentTypeFromName(filename); 
      byte[] out = IOUtils.toByteArray(inputStream); 

      HttpHeaders responseHeader = new HttpHeaders(); 
      responseHeader.add("content-disposition", "attachment; filename=" + filename); 
      responseHeader.add("Content-Type", type); 

      respEntity = new ResponseEntity(out, responseHeader, HttpStatus.OK); 
     } else { 
      respEntity = new ResponseEntity("File Not Found", HttpStatus.NOT_FOUND); 
     } 
     return respEntity; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return respEntity = new ResponseEntity("IO Exception", HttpStatus.INTERNAL_SERVER_ERROR); 
    } 
} 

回答

0

最後,我選擇的文件編碼與Base64和在片作爲JSON文件發送。

相關問題