1. 應(yīng)用場景
??通常用在以文件的形式下載大量數(shù)據(jù)。
2. 寫法舉例
@RestController
public class ExampleController
{
@ApiOperation("下載資源文件")
@ApiImplicitParams({
@ApiImplicitParam(name="id" , value="資源標識" , dataTypeClass=String.class , required = true) ,
@ApiImplicitParam(name="fileName" , value="下載下來的文件的文件名" , dataTypeClass=String.class , required = true)
})
@GetMapping(value="/resource/_download" , produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void downloadResource(@RequestParam("id") String aId
, @RequestParam("fileName") String aFileName
, HttpServletResponse aResponse) throws IOException, DocumentException
{
InputStream ins = xxxx ; //獲取資源
aResponse.setHeader("content-type", "application/octet-stream");
aResponse.setContentType("application/octet-stream");
aResponse.setHeader("Content-Disposition", "attachment;filename=" + aFileName);
// 輸出數(shù)據(jù)
StreamAssist.transfer_1(ins , aResponse.getOutputStream());
}
}
3. 擴展
??如果被下載的資源滿足以下條件,則可以先將數(shù)據(jù)寫到Temp File,然后返回TempFile。
- 存在短時間內(nèi)被重復(fù)獲取的可能性;
- 且資源在這段時間內(nèi)幾乎不變;
- 資源的查詢組織會消耗比較多的資源。