首页 > 编程语言 >生成压缩包的多种方式——Java【上】

生成压缩包的多种方式——Java【上】

时间:2022-12-05 19:01:30浏览次数:64  
标签:Java org 生成 new import byte fileName response 压缩包

天气好冷,不想敲代码呀呀呀~

生成压缩包的多种方式——Java【上】_java

日子还在继续,工作不能停!开始今天正题——文件操作【生成压缩包】如何生成压缩包呢?作为一个后端开发入门的打工人,首先想到还是用后端方式去解决。

说到后端,推荐一个比较厉害的Maven依赖,我以前好像也推荐过,不过没关系,再推荐一遍!​​文档说明链接​

        <dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>

​​​​讲真!人家还是真的有封装好哦!​​压缩工具链接​​ 

生成压缩包的多种方式——Java【上】_spring_02

超级好用!看完案例感觉好像很清楚,不需要额外写啥了...【要我啥用】

package com.example.tool.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


@RestController
@RequestMapping("/util")
public class UtilController {
private static final Log logger = LogFactory.getLog(UtilController.class);

@ApiOperation(value = "本地文件压缩", notes = "文件压缩成压缩包")
@RequestMapping(value = "/zipLocalFile", method = RequestMethod.GET)
public void zipLocalFile(HttpServletResponse response) throws UnsupportedEncodingException {
String localFileUrl = "e:/test.zip";
ZipUtil.zip(FileUtil.file(localFileUrl), false,
FileUtil.file("e:/txt1.txt"),
FileUtil.file("e:/txt2.txt")
);

BufferedInputStream bufferedInputStream = null;
ServletOutputStream servletOutputStream = null;

try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(localFileUrl));
servletOutputStream = response.getOutputStream();
int len;
byte[] bytes = new byte[1024 * 2];
while ((len = bufferedInputStream.read(bytes)) != -1) {
servletOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (servletOutputStream != null) {
servletOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
response.setContentType("application/octet-stream; charset=UTF-8");
}


}

简单写了一个API【这样写,看起来好像有点问题】具体问题在哪先不管,反正也就看起来有点,还是可以运行,不信你自己试试咯。 现在继续接着搞,上面是保存本地文件,那如果要保存网络图片呢?

package com.example.tool.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


@RestController
@RequestMapping("/util")
public class UtilController {
private static final Log logger = LogFactory.getLog(UtilController.class);


public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}


public File getFile(String fileUrl, String fileName) {
// fileName文件名处理
if (fileName.contains("/")) {
fileName = fileName.replace("/", "-");
}
fileName = "e:/" + fileName;
File imageFile = null;
try {
//定义一个URL对象,就是你想下载的URL地址
URL url = new URL(fileUrl);
//打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为10秒
conn.setConnectTimeout(10 * 1000);
//通过输入流获取数据
InputStream is = conn.getInputStream();
//得到二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(is);
//创建一个文件对象用来保存
imageFile = new File(fileName);
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流,释放资源
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}

return imageFile;
}


@ApiOperation(value = "本地文件压缩", notes = "文件压缩成压缩包")
@RequestMapping(value = "/zipLocalFile", method = RequestMethod.GET)
public void zipLocalFile(HttpServletResponse response) throws UnsupportedEncodingException {
String imgUrl = "https://pic.netbian.com/uploads/allimg/221201/005454-1669827294d312.jpg";

String localFileUrl = "e:/test.zip";
ZipUtil.zip(FileUtil.file(localFileUrl), false,
FileUtil.file("e:/txt1.txt"),
FileUtil.file("e:/txt2.txt"),
FileUtil.file(getFile(imgUrl, "img-test.jpg"))
);

BufferedInputStream bufferedInputStream = null;
ServletOutputStream servletOutputStream = null;

try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(localFileUrl));
servletOutputStream = response.getOutputStream();
int len;
byte[] bytes = new byte[1024 * 2];
while ((len = bufferedInputStream.read(bytes)) != -1) {
servletOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (servletOutputStream != null) {
servletOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
response.setContentType("application/octet-stream; charset=UTF-8");
}

}

加了getFile和readInputStream后,突然代码量就上来了,要命!看起来变得有点复杂了。

仔细看看,发现好像又一丢丢问题!

问题一:getFile先把网络图片保存到本地,要是不想保存本地呢?

问题二:请求接口时,ZipUtil.zip已经生成e:/test.zip,但是又会通过response响应文件流给前端,浏览器再下载一份zip文件。

问题好多!我为什么要这样搞【救命~】要放服务器上的,这样额外保存一份,磁盘空间都不够诶【偷偷说:不管够不够,反正敢这样搞,怕是会被打死】

package com.example.tool.controller;

import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.ZipUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


@RestController
@RequestMapping("/util")
public class UtilController {
private static final Log logger = LogFactory.getLog(UtilController.class);


public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}


public File getFile(String fileUrl, String fileName) {
// fileName文件名处理
if (fileName.contains("/")) {
fileName = fileName.replace("/", "-");
}
fileName = "e:/" + fileName;
File imageFile = null;
try {
//定义一个URL对象,就是你想下载的URL地址
URL url = new URL(fileUrl);
//打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为10秒
conn.setConnectTimeout(10 * 1000);
//通过输入流获取数据
InputStream is = conn.getInputStream();
//得到二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(is);
//创建一个文件对象用来保存
imageFile = new File(fileName);
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流,释放资源
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}

return imageFile;
}


@ApiOperation(value = "本地文件压缩", notes = "文件压缩成压缩包")
@RequestMapping(value = "/zipLocalFile", method = RequestMethod.GET)
public void zipLocalFile(HttpServletResponse response) throws UnsupportedEncodingException {
String imgUrl = "https://pic.netbian.com/uploads/allimg/221201/005454-1669827294d312.jpg";

String localFileUrl = "e:/test.zip";
ZipUtil.zip(FileUtil.file(localFileUrl), false,
FileUtil.file("e:/txt1.txt"),
FileUtil.file("e:/txt2.txt"),
FileUtil.file(getFile(imgUrl, "img-test.jpg"))
);

BufferedInputStream bufferedInputStream = null;
ServletOutputStream servletOutputStream = null;

try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(localFileUrl));
servletOutputStream = response.getOutputStream();
int len;
byte[] bytes = new byte[1024 * 2];
while ((len = bufferedInputStream.read(bytes)) != -1) {
servletOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (servletOutputStream != null) {
servletOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
response.setContentType("application/octet-stream; charset=UTF-8");
}


@ApiOperation(value = "网络文件压缩", notes = "压缩文件成压缩包")
@RequestMapping(value = "/zipNetworkFile", method = RequestMethod.GET)
public void zipNetworkFile(HttpServletResponse response) throws IOException {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);

String imgUrl = "https://pic.netbian.com/uploads/allimg/221201/005454-1669827294d312.jpg";
try {
URL url3 = new URL(imgUrl);
InputStream stream = URLUtil.getStream(url3);
FastByteArrayOutputStream read = IoUtil.read(stream);
byte[] bytesImg = read.toByteArray();
zipOutputStream.putNextEntry(new ZipEntry(FileUtil.getName("img-test.jpg")));
zipOutputStream.write(bytesImg);
IOUtils.closeQuietly(read);
zipOutputStream.flush();
zipOutputStream.closeEntry();
} catch (IOException e) {
logger.warn("下载失败");
}
IOUtils.closeQuietly(zipOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();

// 由响应response直接输入到前端
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("test.zip", "UTF-8"));
response.addHeader("Content-Length", "" + bytes.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(bytes, response.getOutputStream());
}


}

zipNetworkFile看起来顺眼多了,但是图片量大会不会有点问题?如果我要给压缩包设置压缩密码怎么搞呢?

标签:Java,org,生成,new,import,byte,fileName,response,压缩包
From: https://blog.51cto.com/u_15426660/5913245

相关文章

  • JNI__贯穿java层与c++层的返回值
    返回值类型的写法和位置不太一样,模仿文件中其他位置的写法即可,如:frameworks/base/services/core/java/com/android/server/pm/Installer.java/**@hide*/publicbooleanc......
  • Java学习十二
    一.小结1.每个容器都有一个布局管理器,它按照所需的位置在容器中定位和放置组件。三个简单且常用的布局管理器是FlowLayout、GridLayout和BorderLayout。2.可以将JPane1作......
  • JavaScript基础1
    今日内容概要JS简介变量与常量基本数据类型运算符流程控制函数内置对象今日内容详细JS简介全称JavaScript但是与Java一毛钱关系都没有之所以这么叫是为了蹭Ja......
  • JavaScript编程语言
    JavaScript编程语言JavaScript(简称“JS”)是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。JavaScript是一种属于网络的高级脚本语言,已经被广泛用于Web应用开......
  • 5:IO Stream-Java API 实战
    (目录)1.相对论和IO流之说Input/OutputStream词典中Stream的意思是有方向性的流动的液体/电流,强调过程理解I/OStream可以看作是输入/输出方向的流体从相对论的角......
  • JavaScript习题之算法设计题
    //1.九九乘法表for(vari=1;i<10;i++){document.write("<span>");for(varj=1;j<=i;j++){if(j%2==0){......
  • JavaScript习题之填空题
    1.JavaScript有两种引⽤数据类型:__数组___、__对象__。2.Javascript通过__setTimeout___延迟指定时间后,去执⾏某程序。3.Javascript⾥String对象通过__indexOf__⽅法取......
  • JavaScript习题之判断题
    1.JavaScript是Java语言的脚本形式。()2.JavaScript中的方法名不区分大小写。()3.JavaScript语句结束时的分号可以省略。()4.通过外链式引入JavaScript时,可以省略</scr......
  • JavaScript习题之简答题
    1.分别描述HTML、CSS、JS在页面组成中的作用。HTML是超文本标记语言,是用来描述网页的语言,定义网页的结构,内容可以包含文字、图片、视频等。CSS是层叠样式表,定义如何显示HT......
  • Java 编程基础01
    一、Java开发环境搭建1、开发工具的下载和安装   1)下载方式一:官网下载www.sun.com     www.oracle.com   2)下载方式二:通过搜索下载www.baidu.c......