java在浏览器弹框读取本地文件另存
public static void download(HttpServletResponse response, String filename) throws IOException {
// 获取服务器文件
File file = new File(filename);
InputStream ins = new FileInputStream(file);
OutputStream os = null;
/* 设置文件ContentType类型,这样设置,会自动判断下载文件类型 */
response.setContentType("multipart/form-data");
/* 设置文件头:最后一个参数是设置下载文件名 */
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
try {
os = response.getOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = ins.read(b)) > 0) {
os.write(b, 0, len);
}
os.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
os.close();
ins.close();
}
}
/**
* 下载模板
*
* @param httpServletRequest
* @param response
* @param name 文件名
* @param path 文件路径
* @return
* @throws Exception
*/
public ServletOutputStream downFileForMoulds(HttpServletRequest httpServletRequest, HttpServletResponse response, String name, String path) throws Exception {
File file = null;
file = new File(path);
if (!file.isFile()) {
file = new File(path, name);
}
//下载显示的文件名,解决中文名称乱码问题
String downloadFielName = null;
if (httpServletRequest.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
downloadFielName = new String(name.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
} else if (httpServletRequest.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0 ||
httpServletRequest.getHeader("User-Agent").indexOf("like Gecko") > 0) {
downloadFielName = URLEncoder.encode(name, "UTF-8");// IE浏览器
} else if (httpServletRequest.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
downloadFielName = new String(name.getBytes("UTF-8"), "ISO8859-1");// 谷歌
}
response.setHeader("Content-Disposition", "attachment; filename=" + downloadFielName);
ServletOutputStream output = null; //定义了一个输出流,用于由Servlet向客户发送二进制数据
BufferedInputStream br = null;
try {
br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
output = response.getOutputStream();
while ((len = br.read(buf)) > 0) {
output.write(buf, 0, len);
}
} catch (Exception e) {
log.error("文件下载异常", e);
throw new Exception("文件下载错误,请联系管理员!");
} finally {
if (br != null) {
br.close();
}
if (output != null) {
output.close();
}
}
return output;
}
两种基本相同,加了一个浏览器校验