JAVA代码实现下载单个文件,和下载打包文件
//下载单个文件调用方法
/**
* response
* imgPath 下载图片地址
* fileName 保存下载文件名称
* @date 2015年4月14日 下午5:53:24
*/
public static void download(HttpServletResponse response,String imgPath,String fileName){
OutputStream out=null;
BufferedInputStream br =null;
try {
// 设置响应类型为下载
response.setContentType("application/x-msdownload;charset=UTF-8");
//页面乱码问题
response.setCharacterEncoding("UTF-8");
//设置下载文件名称
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
//输入流
br = new BufferedInputStream(new FileInputStream(imgPath));
byte[] buf = new byte[1024];
int len = 0;
out = response.getOutputStream();
while ((len = br.read(buf)) > 0){
out.write(buf, 0, len);
out.flush();
}
if(null!=out) out.close();
if(null!=br) br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//下载打包压缩文件调用方法
/**
*files 需要下载问价的File 集合
* @Description: TODO(文件打包下载)
*/
public static HttpServletResponse downLoadFiles(List<File> files,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
ServletContext context=request.getSession().getServletContext();
/**这个集合就是你想要打包的所有文件,
* 这里假设已经准备好了所要打包的文件*/
//List<File> files = new ArrayList<File>();
/**创建一个临时压缩文件,
* 我们会把文件流全部注入到这个文件中
* 这里的文件你可以自定义是.rar还是.zip*/
File file = new File(context.getRealPath("/test.rar"));
if (!file.exists()){
file.createNewFile();
}
response.reset();
//response.getWriter()
//创建文件输出流
FileOutputStream fous = new FileOutputStream(file);
/**打包的方法我们会用到ZipOutputStream这样一个输出流,
* 所以这里我们把输出流转换一下*/
ZipOutputStream zipOut = new ZipOutputStream(fous);
/**这个方法接受的就是一个所要打包文件的集合,
* 还有一个ZipOutputStream*/
zipFile(files, zipOut);
zipOut.close();
fous.close();
return downloadZip(file,response);
}catch (Exception e) {
e.printStackTrace();
}
return response ;
}
/**
* 把接受的全部文件打成压缩包
* @param List<File>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(List files,ZipOutputStream outputStream) {
int size = files.size();
for(int i = 0; i < size; i++) {
File file = (File) files.get(i);
zipFile(file, outputStream);
}
}
public static HttpServletResponse downloadZip(File file,HttpServletResponse response) {
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}finally{
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
/**
* 根据输入的文件与输出流对文件进行打包
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(File inputFile,ZipOutputStream ouputStream) {
try {
if(inputFile.exists()) {
/**如果是目录的话这里是不采取操作的,
* 至于目录的打包正在研究中*/
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 512);
//org.apache.tools.zip.ZipEntry
ZipEntry entry = new ZipEntry(inputFile.getName());
ouputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[1024];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
IN.close();
} else {
try {
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], ouputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
JAVA代码实现下载单个文件,和下载打包文件的更多相关文章
- java代码和spring框架读取xml和properties文件
1.java文件读取properties文件 Properties props = new Properties(); try { //资源文件存放在类文件的根目录下.即是放在src下面.则不需要写路 ...
- java代码判断图片文件格式, 不是根据文件后缀来判断。
public static final String TYPE_JPG = "jpg"; public static final String TYPE_GIF = "g ...
- Linux正则表达式、shell基础、文件查找及打包压缩
Linux正则表达式.shell基础.文件查找及打包压缩 一.正则表达式 Linux正则表达式分为2类: 1.基本正则表达式(BRE) 2.扩展正则表达式(ERE) 两者的区别: 1.使用扩展正则表达 ...
- Linux_打包文件
将多个文件打包成一个大文件,用tar命令 tar是将多个文件前后连接在一起,tar并不对文件进行压缩 tar -cf 要创建的打包文件名(最后加上.tar) 要打包的文件/列表 c代表创 ...
- 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器
引入: 在我们昨天架设好了Samba服务器上并且创建了一个 Samba 账户后,我们就迫不及待的想用JAVA去操作Samba服务器了,我们找到了一个框架叫 jcifs,可以高效的完成我们工作. 实践: ...
- java代码实现ftp服务器的文件上传和下载
java代码实现文件上传到ftp服务器: 1:ftp服务器安装: 2:ftp服务器的配置: 启动成功: 2:客户端:代码实现文件的上传与下载: 1:依赖jar包: 2:sftpTools 工具类: ...
- JAVA代码时间SFTP文件的下载
参考文章:http://blog.csdn.net/smallerpig/article/details/50976191 SFTP文件的下载与FTP文件的下载差别较大,需要下载jsch-0.1.54 ...
- 【Azure Developer】VS Code运行Java 版Azure Storage SDK操作Blob (新建Container, 上传Blob文件,下载及清理)
问题描述 是否可以用Java代码来管理Azure blob? 可以.在代码中加入azure-storage-blob依赖.即可使用以下类操作Azure Storage Blob. BlobServic ...
- Java ftp 上传文件和下载文件
今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...
随机推荐
- Android SDK Manager 中如果没有相应的镜像ARM XX Image
Android SDK Manager 中如果没有相应的镜像ARM XX Image 处理做法是:先更新 相应版本Android SDK Tools 然后出现 ARM XX Image
- 介绍linux下vi命令的使用
功能最强大的编辑器之一——vivi是所有UNIX系统都会提供的屏幕编辑器,它提供了一个视窗设备,通过它可以编辑文件.当然,对UNIX系统略有所知的人,或多或少都觉得vi超级难用,但vi是最基本的编辑器 ...
- MIT 6.828 JOS学习笔记0. 写在前面的话
0. 简介 操作系统是计算机科学中十分重要的一门基础学科,是一名计算机专业毕业生必须要具备的基础知识.但是在学习这门课时,如果仅仅把目光停留在课本上一些关于操作系统概念上的叙述,并不能对操作系统有着深 ...
- hdu5119 dp
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5119 题意: 输入T组数据,每组数据包括两个数n和m,接下来n个数,这n个数可以随意取( ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- 破解ZIP加密文件密码fcrackzip
破解ZIP加密文件密码fcrackzip ZIP是最常见的文件压缩方式.由于其压缩算法开源,主流操作系统都支持这种压缩算法.ZIP压缩方式支持密码加密.加密的时候会在文件头部保存密钥相关信息.利用这个 ...
- BZOJ 3542 [Poi2014]Couriers ——可持久化线段树
[题目分析] 查找区间内出现次数大于一半的数字. 直接用主席树,线段树上维护区间大小,由于要求出现次数大于一半,每到一个节点可以分治下去. 时间复杂度(N+Q)logN [代码] #include & ...
- js-高级技术
高级技术: 1.function Person(name,age,job){ this.name=name; this.age=age; this.job=job; } var person=new ...
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
- STL UVA 11995 I Can Guess the Data Structure!
题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...