@Controller
@RequestMapping("/download")
public class DownloadController {
@RequestMapping(path = "/output1")
public void output1(HttpServletRequest request, HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
String filename = "案例案例.docx";
try {
filename = URLEncoder.encode(filename, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//中文文件名需要編碼
response.setHeader("Content-Disposition", "attachment;filename=" + filename + "");
try {
String filePath = "C:\\\\Users\\\\admin\\\\Desktop\\\\案例案例.docx";
File file = new File(filePath);
response.setHeader("Content-Length", file.length() + "");
InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int r = -1;
while ((r = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, r);
}
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
【java下載文件到本地 java下載文件】}
(今完)
推薦閱讀
- Excel的文件圖標(biāo)打不開了
- 手機亂下載應(yīng)用怎么辦
- 蘋果手機怎么查我之前下載過什么軟件
- pr制作延時攝影教程
- 抖音卸載了從新下載能恢復(fù)原樣嗎
- 文件夾怎么設(shè)置密碼
- 第一創(chuàng)業(yè)證券交易app下載 第一創(chuàng)業(yè)證券交易軟件下載
- 百度網(wǎng)盤的文件怎么下載到電腦
- 繪畫app下載 繪畫app
- 蘋果如何下載軟件
