package com.xu.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
@SuppressWarnings(value = "all")
public class FileUtils {
/**
* 1 多文件下载
*
* @param response HttpServletResponse
* @param files[] 待下载文件
* @param downloadfile 下载文件文件名+文件类型(a.zip)
* @throws Exception
* @date 2019年3月29日11:31:35
*/
public void multFileDownload(HttpServletResponse response, String[] files, String downloadfile) throws Exception {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName=" + downloadfile);
ServletOutputStream out;
FileInputStream instream = null;
try {
ZipOutputStream zipstream = new ZipOutputStream(response.getOutputStream());
for (String file : files) {
if (!new File(file).exists()) {
continue;
}
instream = new FileInputStream(file);
ZipEntry entry = new ZipEntry(file);
zipstream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = 0;
while (len != -1) {
len = instream.read(buffer);
zipstream.write(buffer, 0, buffer.length);
}
instream.close();
zipstream.closeEntry();
zipstream.flush();
}
zipstream.finish();
zipstream.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 2 多文件下载
*
* @param response HttpServletResponse
* @param files[] 待下载文件
* @param downloadfile 下载文件
* @param path 待下载文件路径
* @param tempfile 待下载临时文件
* @throws Exception
* @date 2019年3月29日11:31:35
*/
public void multFileDownload(HttpServletResponse response, String[] files, String path, String downloadfile) throws Exception {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName=" + downloadfile);
ServletOutputStream out;
FileInputStream instream = null;
try {
ZipOutputStream zipstream = new ZipOutputStream(response.getOutputStream());
for (String file : files) {
if (!new File(path + file).exists()) {
continue;
}
instream = new FileInputStream(path + file);
ZipEntry entry = new ZipEntry(path + file);
zipstream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = 0;
while (len != -1) {
len = instream.read(buffer);
zipstream.write(buffer, 0, buffer.length);
}
instream.close();
zipstream.closeEntry();
zipstream.flush();
}
zipstream.finish();
zipstream.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 1 单文件下载
*
* @param response HttpServletResponse
* @param filepath 待下载文件路径
* @param downloadfile 下载文件
* @date 2019年3月29日11:31:35
*/
public void singleFileDownload(HttpServletResponse response, String filepath, String downloadfile) {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName=" + downloadfile);
ServletOutputStream out;
File file = new File(filepath);
if (!file.exists()) {
new RuntimeException("文件不存在");
}
try {
FileInputStream inputStream = new FileInputStream(file);
out = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 2 单文件下载
*
* @param response HttpServletResponse
* @param filepath 待下载文件路径
* @param filename 下载文件名称
* @param filetype 下载文件类型
* @date 2019年3月29日11:31:35
*/
public void singleFileDownload(HttpServletResponse response, String filepath, String filename, String filetype) {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName=" + filename + filetype);
ServletOutputStream out;
File file = new File(filepath);
if (!file.exists()) {
new RuntimeException("文件不存在");
}
try {
FileInputStream inputStream = new FileInputStream(file);
out = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 1 多文件 压缩成 ZIP
*
* @param files 文件名字
* @param path 文件路径
* @param loaclfilepath 临时文件全路径
* @date 2019年3月29日11:31:35
*/
public void toZip(String[] files, String path, String loaclfilepath) throws Exception {
FileInputStream instream = null;
ZipOutputStream zipstream = new ZipOutputStream(new FileOutputStream(new File(loaclfilepath)));
for (String file : files) {
if (!new File(path + file).exists()) {
continue;
}
instream = new FileInputStream(path + file);
ZipEntry entry = new ZipEntry(path + file);
zipstream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = instream.read(buffer)) != -1) {
zipstream.write(buffer, 0, buffer.length);
}
zipstream.closeEntry();
zipstream.flush();
}
zipstream.finish();
zipstream.close();
}
/**
* 2 多文件 压缩成 ZIP
*
* @param files 文件名字
* @param path 文件路径
* @param loaclfilepath 压缩文件全路径
* @date 2019年3月29日11:31:35
*/
public void toZip(List<String> files, String path, String loaclfilepath) throws Exception {
FileInputStream instream = null;
ZipOutputStream zipstream = new ZipOutputStream(new FileOutputStream(new File(loaclfilepath)));
for (String f
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
DynamicWebProject.7z (24个子文件)
pom.xml 803B
target
test-classes
m2e-wtp
web-resources
META-INF
maven
DynamicWebProject
DynamicWebProject
pom.xml 803B
pom.properties 265B
MANIFEST.MF 88B
classes
com
xu
servlet
FileDownloadServlet.class 2KB
package-info.class 120B
utils
FileUtils.class 6KB
package-info.class 118B
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 701B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 572B
org.eclipse.jdt.core.prefs 613B
src
test
java
main
webapp
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 713B
lib
java
com
xu
servlet
package-info.java 23B
FileDownloadServlet.java 2KB
utils
package-info.java 76B
FileUtils.java 8KB
.project 1KB
.classpath 1KB
build
classes
共 24 条
- 1
深色風信子
- 粉丝: 464
- 资源: 41
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0