java 下载文件功能代码例子
public static void down(HttpServletRequest request,
HttpServletResponse
response) throws Exception {
String name="aaa.*";//文件名
String uploadPath =
UploadFileHelper.getRepositoryPath()+"//";//文件来源
String
filePath = name;
String fileName = name;
if
(request.getHeader("User-Agent").toLowerCase().indexOf("firefox")
> 0){
fileName =
new String(fileName.getBytes("UTF-8"),
"ISO8859-1");//firefox浏览器
}else {
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE")
> 0){
fileName = URLEncoder.encode(fileName,
"UTF-8");//IE浏览器
}
}
response.setContentType("text/plain");
response.setHeader("Location",fileName);
response.reset();
response.setHeader("Cache-Control",
"max-age=0" );
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
BufferedInputStream bis = null;
BufferedOutputStream bos =
null;
OutputStream fos = null;
InputStream fis = null;
filePath = uploadPath +
filePath;
fis = new
FileInputStream(filePath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.close();
bis.close();
fos.close();
fis.close();
}
java 下载文件功能代码例子的更多相关文章
- java下载文件工具类
java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- php下载文件的代码示例
php下载文件的代码示例,需要的朋友可以参考下 <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content- ...
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- springboot项目下载文件功能中-切面-导致的下载文件失败的bug
背景:使用spring提供的 ResponseEntity 和Resource结合,实现的下载文件功能 bug:Resource已经加载到了文件, 并且通过 ResponseEntity 构建了响应, ...
- java下载文件时文件名出现乱码的解决办法
转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249 java下载文件时文件名出现乱码的解决办法: String userAgent ...
- 批量去除Teleport Pro整站下载文件冗余代码
teleport pro tppabs标签批量删除 teleport pro tppabs标签批量删除 使 用Teleport Pro下载的网页代码中包含了很多垃圾代码,比如下载的html网页代码中会 ...
- Java 下载文件
public @ResponseBody void exportExcel(HttpServletRequest request, HttpServletResponse response, Khxx ...
- java下载文件demo
java通过http方式下载文件 https://www.cnblogs.com/tiancai/p/7942201.html
- Java下载文件(流的形式)
@RequestMapping("download") @ResponseBody public void download(HttpServletResponse respons ...
随机推荐
- cocos2d-x CCListView
转自:http://blog.csdn.net/onerain88/article/details/7641126 cocos2d-x 2.0 版更新了,把opengl 1.1 替换为opengl 2 ...
- 算法效果AB测试中的PV-UV不对称性
(转载请注明原创于潘多拉盒子) 算法效果的AB测试,是指在相同的应用场景下,对比不同算法的效果.通常的做法是,按照PV或UV随机分配流量到算法上,计算算法的CTR或转化率进行对比.为了表述简单,我们假 ...
- 赵雅智_ContentProvider
ContentProvider介绍 ContentProvider是不同应用程序之间进行交换数据的标志API 也就是说:一个应用程序通过ContentProvider暴露自己的数据操作接口,那么无论该 ...
- Android学习第一课
首先看一个android项目中各个包的作用 以下看几个经常使用的控件: 1. TextView 显示文本框控件 2. EditText 输入文本框 TextView控件经常使用属性: id----控件 ...
- zoj3672 Gao The Sequence
原地踏步了半年,感觉一切都陌生了~ 题意:a[i]-一个任意的数,这个数要等于a[1]~a[i-1]每个数减去任意一个数,经过多次这样的变换到达目标b序列,能到达就yes不能到达距no. 一开始各种分 ...
- JSONP(处理跨域问题)
Ajax直接请求普通文件存在跨域无权限访问的问题 凡是拥有"src"这个属性的标签都拥有跨域的能力,比如<script>.<img>.<iframe& ...
- ASP.NET MVC 之 路由配置
主要操作在App_Start 目录下的 RouteConfig.cs 文件. 一.Url构造方式 1.命名参数规范+匿名对象 routes.MapRoute( name: "Default& ...
- css笔记02:选择器(标签式和类)
body { margin:; padding:; background:#000 url('images/backgrounds/star.png') no-repeat fixed; font: ...
- sql 自定义函数-16进制转10进制
做过笔记,好记性不如烂笔头: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[HEXTOINT]') and ...
- Zabbix Api的使用
API使用 zabbix官网文档:https://www.zabbix.com/documentation/2.2/manual/api, Zabbix API是基于JSON-RPC 2.0规格,具体 ...