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 ...
随机推荐
- 用Spring3编写第一个HelloWorld项目
第一个HelloWorld程序 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclipse IDE的操作 如果你还没有设置好环境 ...
- Java模拟登陆【转载】
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...
- ListView 文件重命名
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Control ...
- delphi 判断是否出现滚动条
delphi 判断是否出现滚动条 if (GetWindowlong(Stringgrid1.Handle, GWL_STYLE) and WS_VSCROLL) <> 0 the ...
- MaterialDesignLibrary
https://github.com/navasmdc/MaterialDesignLibrary MaterialDesignLibrary.zip
- linux对外开放某个端口命令
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /etc/rc.d/init.d/iptables save /etc/rc.d/init.d/ ...
- SVN Cleanup failed的解决办法
一开始没有更新执行了提交操作,提示有冲突 再执行更新操作的时候出现了“之前操作未完成,如果该操作被中断了执行cleanup命令”的提示
- python读取文本、配对、插入数据脚本
#在工作中遇见了一个处理数据的问题,纠结了很久,写下记录一下.#-*- coding:UTF-8 -*- #-*- author:ytxu -*- import codecs, os, sys, pl ...
- 琐碎-关于hadoop2.X那些端口
此文转载http://www.aboutyun.com/thread-7513-1-1.html Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访 ...