第壹步:設置Response頭部(最關鍵)
response.setContentType("application/octet-stream;charset=UTF-8");
// 設置彈出框提示的文件名
response.addHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
第二步:解析輸入流
// 這裏的in為妳的輸入流
BufferedInputStream is = new BufferedInputStream(in);
// 準備緩沖區
byte[] buffer = new byte[4096];
第三步:將輸入流轉換為輸出流
BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
int offset = 0;
while((offset = is.read(buffer, 0, 4096) > -1) {
os.write(buffer, 0, offset)
}
第四步:關閉輸入輸出流
os.close();
is.close();