java 禁止下载文件

来源:百度知道 编辑:UC知道 时间:2024/06/08 11:37:43
用java做的网站, 如pdf,xls,doc,docx等 让用户点击文件后,只能打开浏览,不能下载,各位大哥大姐有没有什么思路阿,最好能提供可参考的源代码,谢谢了~~

代码中用nosave,客户机器上有相对应的阅读器是可以直接打开,但是没安装
相对应的阅读器时还是弹出了下载框了,我要的效果是不管有没有安装都不能下载
代码如下:
byte[] bytes = doc.getContent() ;
if(bytes==null){
bytes = new byte[0] ;
mime = "text/plain" ;
}
response.reset();

response.addHeader("Content-Disposition","nosave; filename=\""+title+".doc\"");
//response.addHeader("DownloadOptions","nosave");

response.setContentType(mime);
OutputStream outStream = response.getOutputStream();

outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();

这些格式的文件,如果都能看,那就是已经下载到了本地,在浏览器显示了

你看看 你的Page指令那个地方那个 有没有问题,要不直接用DreamWave 直接新建一个JSP页面,然后 把Page指令那一行复制到你的程序里面 ,这样子试一下,我以前遇到了类似的问题。呵呵 好运

BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f)); //f是服务器上 的文件

fileName=new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;filename="+fileName);

OutputStream out = resp.getOutputStream();
while((len=bis.read(b))>0){
out.write(b,0,len);
out.flush();
}
out.close();
bis.close();

做个页面,读取文件!