首页 技术 正文
技术 2022年11月20日
0 收藏 759 点赞 4,195 浏览 3805 个字

获取该FileUtil工具类具体演示,公众号内回复fileutil20200501即可。

package com.example.demo.util;import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class FileUtil { //设置压缩包临时文件目录
public static String zip_temp="D:\\java\\zip_temp"; /**
* 上传文件操作
* @param filePath 文件目录 parentPath/fileName
* @param inputStream 文件输入流
* @throws IOException
*/
public static void uploadFile(String filePath, InputStream inputStream) {
// 获取文件的后缀格式
// String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
File destFile = new File(filePath);
// 生成父级目录
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
try {
// 文件存储操作
BufferedInputStream in=new BufferedInputStream(inputStream);
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(destFile));
int len=-1;
byte[] b=new byte[1024];
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
// 关闭相关流
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 下载单个文件
* @param fileName 文件名
* @param filePath 文件路径
* @param response 响应
* @return
*/
public static void downloadFile(String fileName,String filePath, HttpServletResponse response) throws Exception {
//创建该文件对象
File file = new File(filePath);
//判断该文件是否存在
if (file.exists()){//存在
try {
// 获取文件名
fileName = new String(fileName.getBytes(),"iso-8859-1");
// 设置响应头
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
// 把文件按照字节放入响应体中
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null;
OutputStream os = null; //输出流
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
// 关闭相关流
bis.close();
fis.close();
}catch (Exception e){
e.printStackTrace();
throw new Exception("文件下载出错!");
}
}else {//不存在
throw new Exception("该文件不存在"+fileName);
}
} /**
* 下载多个文件,将多个文件打包成压缩包,将压缩包进行下载
* @param fileList 就是把多个文件的路径放到一个list里面
* @param response 响应
* @param zipFileName 压缩包名字
* @return
* @throws Exception
*/
public static void downLoadFiles(String zipFileName, List<File> fileList, HttpServletResponse response) throws Exception {
try {
// 创建一个临时压缩文件,存放临时文件,方法最后有删除临时文件的步骤
String path = FileUtil.zip_temp+File.separator + "zips" +File.separator +zipFileName+".zip";
File zipFile = new File(path);
if (!zipFile.getParentFile().exists()) {
zipFile.getParentFile().mkdirs();
}
// 创建文件输出流
FileOutputStream fous = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(fous);
//向zip文件中添加原始文件
for (File file: fileList){
zipFile(file,zipOut);
}
// 关闭相关流
zipOut.close();
fous.close();
// 清空response
response.reset();
// 下载单个文件的操作
FileUtil.downloadFile(zipFile.getName(),zipFile.getPath(),response);
// 删除临时文件
zipFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 根据输入的文件与输出流对文件进行打包
* @param file 当个文件
* @param zipOut 压缩包输出流
*/
private static void zipFile(File file, ZipOutputStream zipOut) throws Exception {
try {
if (file.exists()) {
FileInputStream IN = new FileInputStream(file);
BufferedInputStream bins = new BufferedInputStream(IN, 1024);
ZipEntry entry = new ZipEntry(file.getName());
zipOut.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[1024];
while ((nNumber = bins.read(buffer)) != -1) {
zipOut.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
IN.close();
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("压缩包某个文件添加异常!");
}
}
}

===API

1.文件上传方法

uploadFile(String filePath, InputStream inputStream);

参数解释

filePath:上传文件存储的路径

inputStream:上传文件的文件输入流,从MultipartFile对象中,通过getInputStream();方法即可获取。

2.单个文件下载方法

downloadFile(String fileName,String filePath, HttpServletResponse response) throws Exception

参数解释

fileName:下载后文件的文件名

filePath:下载文件在服务器的路径位置

response:响应体

3.多个文件下载方法

downLoadFiles(String zipFileName, List<File> fileList, HttpServletResponse response) throws Exception

参数解释

zipFileName:下载后压缩包的文件名

fileList:多个文件组成的集合

response:响应体

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894