日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 打包下载文件_java下载打包下载文件

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 打包下载文件_java下载打包下载文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:對于文件的一些操作

1.創建文件夾

private String CreateFile(String dir) {

File file = new File(dir);

if (!file.exists()) {

//創建文件夾

boolean mkdir = file.mkdir();

} else {

}

return dir;

}

2.復制文件

private void copyFile(File source, File dest) throws IOException {

FileChannel inputChannel = null;

FileChannel outputChannel = null;

try {

inputChannel = new FileInputStream(source).getChannel();

outputChannel = new FileOutputStream(dest).getChannel();

outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

} finally {

inputChannel.close();

outputChannel.close();

}

}

3.刪除文件

private void delFile(File file) {

File[] listFiles = file.listFiles();

if (listFiles != null) {

for (File f : listFiles) {

if (f.isDirectory()) {

delFile(f);

} else {

f.delete();

}

}

}

file.delete();

}

二:controller的文件下載操作

OutputStream out = response.getOutputStream();

byte[] data = FileToZipUtils.createZip(pdir);//這里根據項目里實際的地址去寫? ,FileToZipUtils是一個工具類

response.reset();

//response.setHeader("Content-Disposition","attachment;fileName=file.zip");

response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(pname+".zip", "UTF-8"));

response.addHeader("Content-Length", ""+data.length);

response.setContentType("application/octet-stream;charset=UTF-8");

IOUtils.write(data, out);

out.flush();

out.close();

三:FileToZipUtils工具類

public class FileToZipUtils {

// 日志

private static Logger log = LoggerFactory.getLogger(FileToZipUtils.class);

/***

* 壓縮文件變成zip輸出流

*/

public static byte[] createZip(String srcSource) throws Exception{

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

ZipOutputStream zip = new ZipOutputStream(outputStream);

//將目標文件打包成zip導出

File file = new File(srcSource);

createAllFile(zip, file, "");

IOUtils.closeQuietly(zip);

return outputStream.toByteArray();

}

/***

* 對文件下的文件處理

*/

public static void createAllFile(ZipOutputStream zip, File file, String dir) throws Exception {

//如果當前的是文件夾,則進行進一步處理

if (file.isDirectory()) {

//得到文件列表信息

File[] files = file.listFiles();

//將文件夾添加到下一級打包目錄

zip.putNextEntry(new ZipEntry(dir + "/"));

dir = dir.length() == 0 ? "" : dir + "/";

//循環將文件夾中的文件打包

for (int i = 0; i < files.length; i++) {

createAllFile(zip, files[i], dir + files[i].getName()); //遞歸處理

}

} else { //當前的是文件,打包處理

//文件輸入流

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

ZipEntry entry = new ZipEntry(dir);

zip.putNextEntry(entry);

zip.write(FileUtils.readFileToByteArray(file));

IOUtils.closeQuietly(bis);

zip.flush();

zip.closeEntry();

}

}

/**

* 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,并存放到zipFilePath路徑下

*

* @param sourceFilePath

* :待壓縮的文件路徑

* @param zipFilePath

* :壓縮后存放路徑

* @param fileName

* :壓縮后文件的名稱

* @return

*/

public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {

boolean flag = false;

File sourceFile = new File(sourceFilePath);

FileInputStream fis = null;

BufferedInputStream bis = null;

FileOutputStream fos = null;

ZipOutputStream zos = null;

if (sourceFile.exists() == false) {

log.info("待壓縮的文件目錄:" + sourceFilePath + "不存在.");

} else {

try {

File zipFile = new File(zipFilePath + "/" + fileName + ".zip");

if (zipFile.exists()) {

log.info(zipFilePath + "目錄下存在名字為:" + fileName + ".zip" + "打包文件.");

} else {

File[] sourceFiles = sourceFile.listFiles();

if (null == sourceFiles || sourceFiles.length < 1) {

log.info("待壓縮的文件目錄:" + sourceFilePath + "里面不存在文件,無需壓縮.");

} else {

fos = new FileOutputStream(zipFile);

zos = new ZipOutputStream(new BufferedOutputStream(fos));

byte[] bufs = new byte[1024 * 10];

for (int i = 0; i < sourceFiles.length; i++) {

// 創建ZIP實體,并添加進壓縮包

ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());

zos.putNextEntry(zipEntry);

// 讀取待壓縮的文件并寫進壓縮包里

fis = new FileInputStream(sourceFiles[i]);

bis = new BufferedInputStream(fis, 1024 * 10);

int read = 0;

while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {

zos.write(bufs, 0, read);

}

}

flag = true;

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

throw new RuntimeException(e);

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally {

// 關閉流

try {

if (null != bis)

bis.close();

if (null != zos)

zos.close();

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

}

return flag;

}

/**

* 解壓縮zip包

*

* @param zipFilePath

* 需要解壓的zip文件的全路徑

* @param unzipFilePath

* 解壓后的文件保存的路徑

* @param includeZipFileName

* 解壓后的文件保存的路徑是否包含壓縮文件的文件名。true-包含;false-不包含

*/

@SuppressWarnings("unchecked")

public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception {

if (StringUtils.isNotBlank(zipFilePath) || StringUtils.isNotBlank(unzipFilePath)) {

File zipFile = new File(zipFilePath);

// 如果解壓后的文件保存路徑包含壓縮文件的文件名,則追加該文件名到解壓路徑

if (includeZipFileName) {

String fileName = zipFile.getName();

if (StringUtils.isNotEmpty(fileName)) {

fileName = fileName.substring(0, fileName.lastIndexOf("."));

}

unzipFilePath = unzipFilePath + File.separator + fileName;

}

// 創建解壓縮文件保存的路徑

File unzipFileDir = new File(unzipFilePath);

if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) {

unzipFileDir.mkdirs();

}

// 開始解壓

ZipEntry entry = null;

String entryFilePath = null, entryDirPath = null;

File entryFile = null, entryDir = null;

int index = 0, count = 0, bufferSize = 1024;

byte[] buffer = new byte[bufferSize];

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

ZipFile zip = new ZipFile(zipFile);

Enumeration entries = (Enumeration) zip.entries();

// 循環對壓縮包里的每一個文件進行解壓

while (entries.hasMoreElements()) {

entry = entries.nextElement();

// 構建壓縮包中一個文件解壓后保存的文件全路徑

entryFilePath = unzipFilePath + File.separator + entry.getName();

// 構建解壓后保存的文件夾路徑

index = entryFilePath.lastIndexOf(File.separator);

if (index != -1) {

entryDirPath = entryFilePath.substring(0, index);

} else {

entryDirPath = "";

}

entryDir = new File(entryDirPath);

// 如果文件夾路徑不存在,則創建文件夾

if (!entryDir.exists() || !entryDir.isDirectory()) {

entryDir.mkdirs();

}

// 創建解壓文件

entryFile = new File(entryFilePath);

if (entryFile.exists()) {

// 檢測文件是否允許刪除,如果不允許刪除,將會拋出SecurityException

SecurityManager securityManager = new SecurityManager();

securityManager.checkDelete(entryFilePath);

// 刪除已存在的目標文件

entryFile.delete();

}

// 寫入文件

bos = new BufferedOutputStream(new FileOutputStream(entryFile));

bis = new BufferedInputStream(zip.getInputStream(entry));

while ((count = bis.read(buffer, 0, bufferSize)) != -1) {

bos.write(buffer, 0, count);

}

bos.flush();

bos.close();

}

zip.close();// 切記一定要關閉掉,不然在同一個線程,你想解壓到臨時路徑之后,再去刪除掉這些臨時數據,那么就刪除不了

}

}

}

總結

以上是生活随笔為你收集整理的java 打包下载文件_java下载打包下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。