首页 / JAVA / JAVA 大文件压缩极速下载
JAVA 大文件压缩极速下载
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVA 大文件压缩极速下载,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6887字,纯文字阅读大概需要10分钟。
内容图文
![JAVA 大文件压缩极速下载](/upload/InfoBanner/zyjiaocheng/770/686695e596384fab86e832ccb30051a1.jpg)
JAVA 大文件压缩极速下载
前言
之前大/多文件压缩 900M下载需要7分钟左右,是因为没有优化代码,网上的代码直接复制粘贴能跑就OK,后来因为需求要求大文件1分钟左右下载完,于是各种百度,各种试验,最终产出了2种方案,一种是找迅雷接口,借助迅雷将多文件大文件下载,这种效率很高,而且不占JAVA JVM内耗,但老板觉得用这软件还得装一个插件,于是另想办法,这个方法于是横空出世,900M 压缩仅20秒左右,下载到本地是看网络流量大小。
这里感谢这位博主,大家可以看看这位博主的,我是直接在此博主的代码基础上改进了以迎合我的需求。
https://blog.csdn.net/ahau10/article/details/52550430/
以下直接代码
以下是controller代码(不重要) 代码片
.
@ResponseBody //高速压缩下载 2019.6.20
@RequestMapping(value = "/downloadfileorfolderforzip")
public void downloadfileorfolderforzip(String urlsStr, HttpServletResponse response, HttpSession session, HttpServletRequest request) throws Exception {
Cookie[] cookies = request.getCookies();
if (null==cookies) {
System.out.println("没有cookie==============");
} else {
for(Cookie cookie : cookies){
if(cookie.getName().equals("downloadstatus")){
cookie.setValue(null);
cookie.setMaxAge(0);// 立即销毁cookie
cookie.setPath("/");
System.out.println("被删除的cookie名字为:"+cookie.getName());
response.addCookie(cookie);
break;
}
}
}
String[] urlss = urlsStr.split(",");
List<String> urls = new ArrayList<String>();
for(String str : urlss) {
urls.add(str);
}
String filename = new Date().getTime() + ".zip";
final File result = new File(this.finalDirPath + "linshi/" + filename);
new HighEffiCompressZipTest().createZipFile(urls, result);
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
try {
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), "iso-8859-1"));
byte[] buff = new byte[1024];
outputStream = response.getOutputStream();
FileInputStream fis = new FileInputStream(result);
bufferedInputStream = new BufferedInputStream(fis);
int num = bufferedInputStream.read(buff);
Cookie cookie = new Cookie("downloadstatus", String.valueOf(new Date().getTime()));
cookie.setMaxAge(5 * 60);// 设置为30min
cookie.setPath("/");
response.addCookie(cookie);
while (num != -1) {
outputStream.write(buff, 0, num);
outputStream.flush();
num = bufferedInputStream.read(buff);
}
System.out.println("==================complete========");
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
throw e;
} finally {
System.out.println("==================complete========");
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
}
}
以下是压缩代码(不重要) 代码片
.
public class HighEffiCompressZipTest {
public static void main(String[] arg) throws Exception {
long begin = System.currentTimeMillis();
System.out.println(new Date());
final File result = new File("E:/myFile/test.zip");
List<String> urls = new ArrayList<String>();
urls.add("C:\\Users\\Administrator\\Desktop\\test\\000001 (1).exe");
urls.add("C:\\Users\\Administrator\\Desktop\\test\\000001 (1) - 副本.exe");
urls.add("C:\\Users\\Administrator\\Desktop\\test\\000001 (2) - 副本.exe");
urls.add("C:\\Users\\Administrator\\Desktop\\test\\000001 (3) - 副本.exe");
new HighEffiCompressZipTest().createZipFile(urls, result);
long end = System.currentTimeMillis();
System.out.println("用时:" + (end - begin) + " ms");
System.out.println(new Date());
}
class CustomInputStreamSupplier implements InputStreamSupplier {
private File currentFile;
public CustomInputStreamSupplier(File currentFile) {
this.currentFile = currentFile;
}
@Override
public InputStream get() {
try {
// InputStreamSupplier api says:
// 返回值:输入流。永远不能为Null,但可以是一个空的流
return currentFile.isDirectory() ? new NullInputStream(0) : new FileInputStream(currentFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
private void addEntry(String entryName, File currentFile, HighEffiCompressZip scatterSample) throws IOException {
ZipArchiveEntry archiveEntry = new ZipArchiveEntry(entryName);
archiveEntry.setMethod(ZipEntry.DEFLATED);
final InputStreamSupplier supp = new CustomInputStreamSupplier(currentFile);
scatterSample.addEntry(archiveEntry, supp);
}
private void compressCurrentDirectory(List<String> dir, HighEffiCompressZip scatterSample) throws IOException {
if (dir == null) {
throw new IOException("源路径不能为空!");
}
String relativePath = "";
// // if (dir.isFile()) {
// // relativePath = dir.getName();
// addEntry("download", null, scatterSample);
// return;
// }
// // 空文件夹
// if (dir.listFiles().length == 0) {
// relativePath = dir.getAbsolutePath().replace(scatterSample.getRootPath(), "");
// addEntry(relativePath + File.separator, dir, scatterSample);
// return;
// }
for (String file : dir) {
File f = new File(file);
if (!f.isDirectory()){
relativePath = f.getParent().replace(scatterSample.getRootPaths(), "");
addEntry(relativePath + File.separator + f.getName(), f, scatterSample);
}
}
}
public void createZipFile(final List<String> rootPaths, final File result) throws Exception {
File dstFolder = new File(result.getParent());
if (!dstFolder.isDirectory()) {
dstFolder.mkdirs();
}
final HighEffiCompressZip scatterSample = new HighEffiCompressZip("zip");
compressCurrentDirectory(rootPaths, scatterSample);
final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(result);
scatterSample.writeTo(zipArchiveOutputStream);
zipArchiveOutputStream.close();
}
}
public class HighEffiCompressZip {
private String rootPaths;
ParallelScatterZipCreator scatterZipCreator = new ParallelScatterZipCreator();
// ParallelScatterZipCreator api says:
// 注意这个类不保证写入到输出文件的顺序。需要保持特定顺序的(manifests,文件夹)必须使用这个类的客户类进行处理
// 通常的做法是 在调用这个类的writeTo方法前把这些东西写入到ZipArchiveOutputStream
ScatterZipOutputStream dirs = ScatterZipOutputStream
.fileBased(File.createTempFile("whatever-preffix", ".whatever"));
public HighEffiCompressZip(String rootPaths) throws IOException {
this.rootPaths = rootPaths;
}
public HighEffiCompressZip() throws IOException {
}
public void addEntry(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier streamSupplier)
throws IOException {
if (zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink()) {
dirs.addArchiveEntry(ZipArchiveEntryRequest.createZipArchiveEntryRequest(zipArchiveEntry, streamSupplier));
} else {
scatterZipCreator.addArchiveEntry(zipArchiveEntry, streamSupplier);
}
}
public void writeTo(final ZipArchiveOutputStream zipArchiveOutputStream)
throws IOException, ExecutionException, InterruptedException {
dirs.writeTo(zipArchiveOutputStream);
dirs.close();
scatterZipCreator.writeTo(zipArchiveOutputStream);
}
public String getRootPaths() {
return rootPaths;
}
public void setRootPaths(String rootPath) {
this.rootPaths = rootPaths;
}
}
内容总结
以上是互联网集市为您收集整理的JAVA 大文件压缩极速下载全部内容,希望文章能够帮你解决JAVA 大文件压缩极速下载所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。