现然页面上有一个链接,是链接到了一个扩展名为kdh的文件,现在点击链接是将kdh文件直接在IE中以乱码形式打开了,怎么能才让点击是下载??是在tomcat中设置吗??
解决方案 »
- 哎呀!哎呀 怎么回事啊 到底哪错了??
- 验证码问题,求高手帮忙
- 动态下拉框关联问题
- tomcat部署项目问题
- 初学spring,希望大家给点意见,我的applicationcontext.xml如下
- jsp开发问题
- JSF 2.0 Ajax Validation
- jsp网站开发步骤请教!!
- [日期] 请问如何计算这个日期?!
- 关于<s:form 警告 No configuration found for the specified action的问题
- 谁帮我看看这段代码老报异常java.util.ConcurrentModificationException
- 怎样保存客户端的HTML代码到后台
response.setContentType( "Application;charset=UTF-8");
File file = new File("../.....kdh");
FileInputStream fis = new FileInputStream(file);
byte[] result = new byte[fis.available()];
fis.read(result);
OutputStream of = response.getOutputStream();
of.write(result);
of.flush();不用设置tomcat
类似与TXT之类文件下载的,你得写下载类哈。要不直接肯定直接IE打开
/**
* 下载文件
*/
public void ExpZSSJ(){
OutputStream os = null;
String s = "application/octet-stream";
try {
os = getResponse().getOutputStream();
getResponse().setContentType(s);
//设置默认下载文件名
String fileName = URLEncoder.encode(*******.*", "UTF-8");
// 设置文件下载头
getResponse().addHeader("Content-Disposition", "attachment;filename="
+ fileName);
drzssjservice.ExpZSData(os,getJhbm(),getJhdm());
os.close();
} catch (IOException e) {
}
}
这个参数设置attachment就是默认不打开。另外如果不行就要看看你的ie的设置了。
getResponse().addHeader("Content-Disposition", "attachment;filename="
+ fileName);
的这个,本意是想用红色提示你这个设置。但是颜色没有出来。不要受误导。
[color=#FF0000]attachment;
正确的写法:
getResponse().addHeader("Content-Disposition", "attachment;filename="
+ fileName);
首先要以 下载字节流的方式:
application/octet-stream
其次Content-Disposition属性设置
////attachment --- 作为附件下载
////inline --- 在线打开