新增了 流下载方法

This commit is contained in:
wanggeng888 2021-05-11 17:35:38 +08:00
parent 5852034f7e
commit fb848e7400

View File

@ -88,4 +88,25 @@ public class RequestUtil {
}
}
/**
* 下载
*
* @param response
* @param inputStream
* @param outFileName
* @throws IOException
*/
public static void download(HttpServletResponse response, InputStream inputStream, String outFileName) throws IOException {
try (OutputStream outputStream = response.getOutputStream()) {
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(outFileName, "UTF-8"));
byte[] buf = new byte[1024];
for (int readLength; (readLength = inputStream.read(buf)) > 0; ) {
outputStream.write(buf, 0, readLength);
}
outputStream.flush();
}
}
}