@ResponseBody
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET}, path = "/bb/test")
void upLoadIpa(HttpServletResponse response,
@RequestParam(value = "id", required = true) Integer id
) {
//全路径,根目录起
downloadFile(ReportPath(), response);
}
/**
* 文件下载.
*
* @param strPath 文件路径
* @param response response
*/
public static void downloadFile(String strPath, HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
// response.setContentType("application/octet-stream");
BufferedInputStream fis = null;
OutputStream toClient = null;
try {
File file = new File(strPath);
// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
// 清空response
response.reset();
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
String strHeaderKey = "Content-Disposition";
String strHeaderValue = String.format("attachment; filename=\"%s\"", file.getName());
response.setHeader(strHeaderKey, new String(strHeaderValue.getBytes(StandardCharsets.UTF_8),
StandardCharsets.ISO_8859_1));
toClient.write(buffer);
} catch (IOException ex) {
logger.error("error", ex);
} finally {
if (null != toClient) {
try {
toClient.flush();
toClient.close();
} catch (IOException e) {
logger.info("", e);
}
}
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
logger.info("", e);
}
}
}
}
浏览器下载服务器文件
最新推荐文章于 2023-03-09 16:02:56 发布