java中使用输出流下载文件弹出打开保存框

try {
String file= request.getRealPath("项目中文件路径");
InputStream is = new FileInputStream(file);
response.reset(); // 必要地清除response中的缓存信息
response.setHeader("Content-Disposition", "attachment; filename=" + file);
response.setContentType("application/vnd.ms-excel");//根据个人需要,这个是下载文件的类型
javax.servlet.ServletOutputStream out = response.getOutputStream();
byte[] content = new byte[1024];
int length = 0;
while ((length = is.read(content)) != -1) {
out.write(content, 0, length);
}
out.write(content);
out.flush();
out.close();
} catch (Exception e) {
handleAjaxException(e, "下载", true);
}


你可能感兴趣的:(java,File,项目,下载文件,content)