要件:フロントデスクのリンクをクリックすると、ブラウザが直接ダウンロードします。
1.背景コード
/**
* 下载文件
*
* @param response 文件流
* @throws Exception 流异常
*/
@ApiModelProperty(value = "下载文件")
@GetMapping("downFile")
public void downFile(HttpServletResponse response) throws Exception {
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; fileName=" + fileInfo.getFullFileName());
File file = new File("D:\\file\\1.jpg");
byte[] bytes = Files.toByteArray(file);
out.write(bytes);
out.close();
}
2.フロントデスクコード
<a href="http://127.0.0.1:8080/apis/file/downFile" rel="nofollow">下载文件</a>
3.整理する
「attachment; fileName = xxx」への応答のヘッダーに「Content-Disposition」を設定します。xxxはファイル名、サフィックス付きのファイル名です。設定後、ダウンロードしたファイルの名前は直接設定の名前になります。