当浏览器 直接打开txt,png等时是直接读取而不是下载时解决方法
2015-11-18 09:59
676 查看
当我们把文件上传到项目 后 数据库会存入上传文件路径,在页面上时就会通过location.href='路径' 来直接下载 但是这样下载txt等文件时 浏览器会直接打开
所以换了种方式 ,通过流来进行下载,
而在返回的response必须加上头 this.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); 告诉浏览器是附件
再设置输出类型 告诉浏览器要下载文件 this.getResponse().setContentType("application/octet-stream");
package com.controller;
import com.tis.helper.Utility;
import com.tis.mvc.ActionResult;
import com.tis.mvc.Controller;
import com.tis.mvc.HttpMethod;
import java.io.*;
/**
*通过流来下载 再加标头
* @author ysc
*/
public class FileZipController extends Controller {
@HttpMethod.HttpGet
public ActionResult downfile()
{
String path =Utility.getRootPath()+"/"+Utility.getRequestStr("fileRpath");
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
this.getResponse().reset();
// 设置response的Header
this.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
this.getResponse().addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(this.getResponse().getOutputStream());
this.getResponse().setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return ActionResult.content("");
}
}
所以换了种方式 ,通过流来进行下载,
而在返回的response必须加上头 this.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); 告诉浏览器是附件
再设置输出类型 告诉浏览器要下载文件 this.getResponse().setContentType("application/octet-stream");
package com.controller;
import com.tis.helper.Utility;
import com.tis.mvc.ActionResult;
import com.tis.mvc.Controller;
import com.tis.mvc.HttpMethod;
import java.io.*;
/**
*通过流来下载 再加标头
* @author ysc
*/
public class FileZipController extends Controller {
@HttpMethod.HttpGet
public ActionResult downfile()
{
String path =Utility.getRootPath()+"/"+Utility.getRequestStr("fileRpath");
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
this.getResponse().reset();
// 设置response的Header
this.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
this.getResponse().addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(this.getResponse().getOutputStream());
this.getResponse().setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return ActionResult.content("");
}
}
相关文章推荐
- Android Manifest 用法
- Android学习笔记(二九):嵌入浏览器
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- Scrapy的架构介绍
- 发布一个自己做的jsp博客系统
- 小白观察:微软释放出其基于 Chromium 的 Edge 浏览器
- 提供个 全免杀海洋2006asp木马 下载
- 高手写的Tracer-Flash代码调试类代码下载
- 浏览器 cookie 限制
- Vista 防火墙 Vista Firewall Control v1.0.11 下载
- 国外Lightbox v2.03.3 最新版 下载
- 火影漫画下载助手 下载
- USBkill U盘防火墙 v3.0 下载
- 腾讯 Tencent Traveler v3.4 下载
- 查杀软件 360安全卫士 v3.2.1.1001 下载
- Kaspersky 6.0.2.666 MP2 nct Release+汉化补丁 下载
- Symantec Norton Ghost v12.0 Retail ISO 多国语言版 下载
- 玩转浏览器IE7的5个顶级使用技巧
- 字符集导致的浏览器跨站脚本攻击分析