java前台怎样获取后台生成的压缩文件件,能不能生成压缩文件后手动选择要存储到哪里

java前台怎样获取后台生成的压缩文件件,能不能生成压缩文件后手动选择要存储到哪里,第1张

后台压缩以后有两种选择,一个放在web本身的目录里,只要提供地址就可以下载了。

另一个是放在非web目录里。通过servlet读取后,以2进制流输出到前台。两种方式都很多。

至于手动存储,所有下载文件都是可以选择存储位置的吧。当然如果你是指服务器的存储,那么就不可能了。不过可以通过代码把文件移动或copy到指定目录。

写了一个 楼主试试吧

import javaioFile;

import javaioFileInputStream;

import javaioFileNotFoundException;

import javaioFileOutputStream;

import javaioIOException;

import javautilzipZipEntry;

import javautilzipZipOutputStream;

public class TestZip {

public static void main(String[] args) {

String srcFilePath = "f:/sqltxt";

String zipFilePath = "f:/outfilezip";

zipFile(srcFilePath, zipFilePath);

}

/

压缩文件

@param srcFilePath 需要压缩的文件的完整路径

@param zipFilePath 压缩生成的文件的路径

/

private static void zipFile(String srcFilePath, String zipFilePath) {

if(srcFilePath == null){

throw new RuntimeException("需要压缩的文件的完整路径 不能为空!");

}

if(zipFilePath == null){

throw new RuntimeException("压缩生成的文件的路径 不能为空!");

}

ZipOutputStream zout = null;

FileInputStream fin = null;

try{

File txtFile = new File(srcFilePath);

fin = new FileInputStream(txtFile);

}catch (FileNotFoundException e) {

throw new RuntimeException("压缩失败!找不到文件" + srcFilePath);

}finally {

try {

finclose();

} catch (Exception e) {

}

}

try {

zout = new ZipOutputStream(new FileOutputStream(new File(zipFilePath)));

File srcFile = new File(srcFilePath);

fin = new FileInputStream(srcFile);

byte[] bb = new byte[4096];

int i = 0;

zoutputNextEntry(new ZipEntry(srcFilegetName()));

while ((i = finread(bb)) != -1) {

zoutsetLevel(9);

zoutwrite(bb, 0, i);

}

} catch (IOException e) {

throw new RuntimeException("压缩失败!", e);

} finally {

try {

zoutclose();

finclose();

} catch (Exception e) {

}

}

}

}

String path = "F:/BaiduYunDownload/JavaWeb程序开发进阶/第九章  自定义标签zip";

File file = new File(path);

if(fileexists()){

Systemoutprintln(filegetParentFile()getName() + ";" + filegetName());

}

在JavaWeb项目中,可以使用ZipInputStream类来实现zip文件的导入。通过创建一个ZipInputStream对象,指定要导入zip文件的路径,然后调用getNextEntry()方法读取zip文件中的内容,最后将zip文件中的内容导入到指定的路径中即可。

以上就是关于java前台怎样获取后台生成的压缩文件件,能不能生成压缩文件后手动选择要存储到哪里全部的内容,包括:java前台怎样获取后台生成的压缩文件件,能不能生成压缩文件后手动选择要存储到哪里、怎样用Java生成ZIP文件、在java中,我想取一段路径中的最后一个文件夹和文件名的路径,怎么取等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9618657.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存