JAVA WEB用servlet下载文件不能弹出对话框 response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName) + "\"");试试 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 lz用下面的代码试试,以下代码再电脑上的各种浏览器都是可以用的, 而且下载输出的文件名可以是中文在android手机浏览器使用,貌似有点问题/** * 下载 * @param filePath 文件路径(物理路径) * @param fileName 输出的文件名 */ public static void downLoad(String filePath, String fileName) { downLoad(new File(filePath), fileName); } /** * 下载 * @param file 文件 * @param fileName 输出的文件名 */ public static void downLoad(File file, String fileName) { try { if (file.exists() && file.isFile()) { downLoad(new FileInputStream(file), fileName, file.length()); } } catch (Exception e) { log.error("下载", e); throw new MvcException(e.getMessage()); } } /** * 下载 * @param input 输入流 * @param fileName 输出的文件名 * @param fileSize 文件大小 */ public static void downLoad(InputStream input, String fileName, long fileSize) { try { HttpServletResponse response = HttpThread.getResponse();// 线程池中取出response String agent = HttpThread.getRequest().getHeader("USER-AGENT").toLowerCase(); String name = new String(fileName.getBytes(agent.indexOf("msie") != -1 ? "GBK" : CharEncoding.UTF_8), CharEncoding.ISO_8859_1); response.setHeader("Content-disposition", "attachment;filename=" + name); response.setContentType("application/octet-stream"); response.setHeader("Content-Length", "" + fileSize); OutputStream output = response.getOutputStream(); byte[] buffer = new byte[1024];int i = 0; while ((i = input.read(buffer)) != -1) { output.write(buffer, 0, i); } input.close();output.flush();output.close(); } catch (Exception e) { log.error("下载", e); } } 再加一句:response.setContentType("application/x-download"); 这个貌似还是不行,是不是前端有什么细节没注意到?楼主是用ajax提交的吗,不能用ajax提交的哦,只能用类似 window.location.href 之类的 那么有可能是你前端问题 BufferedInputStream bi = new BufferedInputStream(new FileInputStream(tempFileName)); OutputStream os = response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.setContentType("application/x-msdownload"); byte buffer[] = new byte[1024]; int size; while ((size = bi.read(buffer, 0, buffer.length)) != -1) { os.write(buffer, 0, size); } bi.close(); os.close(); 这个貌似还是不行,是不是前端有什么细节没注意到?楼主是用ajax提交的吗,不能用ajax提交的哦,只能用类似 window.location.href 之类的 正解,我就是用的ajax提交的,改成window.location.href提交后顺利完成 JavaScript两个数字比较出问题了!!! J2EE 执行main方法得到hibernate实例。大家帮忙在线给分。 struts2中的struts.xml开发模式问题 急急急,求各位帮忙 showModalDialog()在不关闭的情况下,让父窗口得到值 关于JAIN SLEE 请教关于tomcat的域名配置(50分,一定给) 看了jive的问题?? bigint类型的数据该怎么提取? 现在的邮件系统被改? WebLogic安装、项目部署求详解!!!!!!!!!!!!!!急 使用jdbc连接服务器上的mysql出错,但是本地正常
以下代码再电脑上的各种浏览器都是可以用的, 而且下载输出的文件名可以是中文
在android手机浏览器使用,貌似有点问题
/**
* 下载
* @param filePath 文件路径(物理路径)
* @param fileName 输出的文件名
*/
public static void downLoad(String filePath, String fileName) {
downLoad(new File(filePath), fileName);
}
/**
* 下载
* @param file 文件
* @param fileName 输出的文件名
*/
public static void downLoad(File file, String fileName) {
try {
if (file.exists() && file.isFile()) {
downLoad(new FileInputStream(file), fileName, file.length());
}
} catch (Exception e) {
log.error("下载", e);
throw new MvcException(e.getMessage());
}
}
/**
* 下载
* @param input 输入流
* @param fileName 输出的文件名
* @param fileSize 文件大小
*/
public static void downLoad(InputStream input, String fileName, long fileSize) {
try {
HttpServletResponse response = HttpThread.getResponse();// 线程池中取出response
String agent = HttpThread.getRequest().getHeader("USER-AGENT").toLowerCase();
String name = new String(fileName.getBytes(agent.indexOf("msie") != -1 ? "GBK" : CharEncoding.UTF_8), CharEncoding.ISO_8859_1);
response.setHeader("Content-disposition", "attachment;filename=" + name);
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", "" + fileSize);
OutputStream output = response.getOutputStream();
byte[] buffer = new byte[1024];int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
input.close();output.flush();output.close();
} catch (Exception e) {
log.error("下载", e);
}
}
楼主是用ajax提交的吗,不能用ajax提交的哦,只能用类似 window.location.href 之类的
OutputStream os = response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentType("application/x-msdownload");
byte buffer[] = new byte[1024];
int size;
while ((size = bi.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, size);
}
bi.close();
os.close();
楼主是用ajax提交的吗,不能用ajax提交的哦,只能用类似 window.location.href 之类的
正解,我就是用的ajax提交的,改成window.location.href提交后顺利完成