JAVA文件下载,页面显示另存为效果
经过测试 firefox、QQ、IE 浏览器是可以的 chrome浏览器不行(直接下载了)
1. 系统框架springmvc+jsp
2. 后台servlet代码
@RequestMapping("download")
public void download(HttpServletRequest request,HttpServletResponse response){
BufferedInputStream dis = null;
BufferedOutputStream fos = null;
String urlString = request.getParameter("urlString");
String fileName = urlString.substring(urlString.lastIndexOf('/') + 1);
try {
URL url = new URL(urlString);
//response.setContentType("application/x-msdownload;");
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(url.openConnection().getContentLength()));
dis = new BufferedInputStream(url.openStream());
fos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = dis.read(buff, 0, buff.length))) {
fos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dis != null)
try{
dis.close();
}catch (Exception e){
e.printStackTrace();
}
if (fos != null)
try{
fos.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
3. 页面jsp代码
<input type="button" onclick="downloadImage()" value="下载"> <script>
function downloadImage(){
var urlString = "http://pic32.nipic.com/20130829/12906030_124355855000_2.png";
//跳转到后端控制器
location.href="${ctx}/guest/download.do?urlString="+urlString;
}
</script>
4. 下载效果1(firefox)

下载效果2(QQ)

JAVA文件下载,页面显示另存为效果的更多相关文章
- uploadify 配置后,页面显示无效果
uploadify使用的是Flash版本 谷歌浏览器:默认没有开启Flash,进行如下图设置即可
- jQuery实现Ajax请求时,页面显示等待的效果,超过指定请求时间后,进行其他操作
背景:有一个按钮,点击之后向后端程序发起Ajax请求,在请求结果没有返回之前,页面显示等待的效果,此时仍旧是异步请求,等待的效果在接收到结果后撤销. 需求:因为网络延迟或者后端程序的问题,在发起Aja ...
- `<jsp:getProperty>`动作和`<jsp:setProperty>`动作的使用在一个静态页面填写图书的基本信息,页面信息提交给其他页面,并且在其页面显示。要去将表单元素的值赋值给Java
<jsp:getProperty>动作和<jsp:setProperty>动作的使用 1.<jsp:getProperty>动作 语法格式: <jsp:get ...
- java根据年月显示每周
一.页面效果 1.展示7月份的所有周. 2.当前时间2018.08.02 , 显示到本周. 二.前端代码 1.显示层的代码 <span id="weekyear"> ...
- javascript超过容器后显示省略号效果(兼容一行或者多行)
javascript超过容器后显示省略号效果 在实际的项目中,由于文字内容的长度不确定性和页面布局的固定性,难免会出现文字内容超过div(或其他标签,下同)区域的情况,此时比较好的做法就是 ...
- 转:jsp页面显示中文乱码解决方案
jsp页面显示中文乱码: jsp页面的编码方式有两个地方需要设置: <%@ page language="java" import="java.util.*&quo ...
- html页面显示div源代码:用<xmp></xmp>标签
html页面显示div源代码:用<xmp></xmp>标签效果还可以.
- Easyui弹出窗体在iframe的父级页面显示
今天做EasyUI学习的预到了一个这样的问题:通过iframe加载的一个页面在调用$.messager.alert();这个方法后只能在iframe中显示alert效果而不是在全局的页面上显示这并不我 ...
- PHP + JavaScript + Ajax 实现无刷新页面加载效果
数据源工厂 Json生成方式1 Json生成方式2 数据搬运工 数据加工师 转换类型 加工展示 结果展示 初始页面 点击按钮之后 总结 今天这个实验的思路就是实现一个无刷新的页面加载效果.具体的思路是 ...
随机推荐
- log4j中将SocketAppender将日志内容发送到远程服务器
1.服务端配置 1)服务端配置文件log4j-server.properties #Define a narrow log category. A category like debug will p ...
- 疯狂Html+CSS+JS 中JS总结
来自:http://mzkmzk.github.io/blog/2015/10/05/amazeing-js/ 0 总结 本书的JS 第一章有讲语法有挺多常见的坑点和原理解释很不错 第二章DOM编程讲 ...
- Centos7 下Boost 1.61.0源码 配置开发环境
1 下载地址 https://sourceforge.net/projects/boost/files/boost/1.61.0/ boost_1_61_0.tar.gz 2 卸载系统自带的boost ...
- Linux基础命令(四)
作业一:1) 开启Linux系统前添加一块大小为15G的SCSI硬盘 [root@bogon ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 byte ...
- 兼容获取元素当前样式 currentStyle || getComputedStyle
function getStyle(ele, attr) { return ele.currentStyle ? ele.currentStyle[attr] : window.getComputed ...
- 笔记-django学习链接
form表单学习 https://www.cnblogs.com/weiman3389/p/6225075.html python字符串replace()方法 https://www.cnblogs. ...
- Django继承
Django目前支持两种不同的继承方式,包括抽象基础类和多表继承. 1.抽象基础类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 cla ...
- MySQL数据库(5)- pymysql的使用、索引
一.pymysql模块的使用 1.pymysql的下载和使用 之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,那如何在python程序中操作数据库呢?这就需要用到pymysql ...
- CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...
- 我的Android进阶之旅------>Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法
今天使用第三方Jar包afinal.jar时候.想看一下源码,无法看 然后像加入jar相应的源代码包.也无法加入相应的源代码,报错例如以下:The current class path entry b ...