13 changed files with 239 additions and 31 deletions
@ -0,0 +1,121 @@ |
|||||||
|
package com.biutag.supervision.util; |
||||||
|
|
||||||
|
import com.biutag.supervision.service.FileService; |
||||||
|
import jakarta.servlet.http.HttpServletRequest; |
||||||
|
import jakarta.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import java.io.*; |
||||||
|
import java.net.HttpURLConnection; |
||||||
|
import java.net.URL; |
||||||
|
import java.net.URLEncoder; |
||||||
|
import java.nio.file.*; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
import java.util.zip.ZipEntry; |
||||||
|
import java.util.zip.ZipOutputStream; |
||||||
|
|
||||||
|
public class DownloadAndZip { |
||||||
|
|
||||||
|
|
||||||
|
// 下载文件并保存到指定目录
|
||||||
|
public static void downloadFile(InputStream inputStream, String savePath) throws IOException { |
||||||
|
|
||||||
|
|
||||||
|
try ( |
||||||
|
FileOutputStream outputStream = new FileOutputStream(savePath)) { |
||||||
|
|
||||||
|
byte[] buffer = new byte[1024]; |
||||||
|
int bytesRead; |
||||||
|
while ((bytesRead = inputStream.read(buffer)) != -1) { |
||||||
|
outputStream.write(buffer, 0, bytesRead); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// 将文件移动到指定文件夹
|
||||||
|
public static void moveFileToFolder(String sourcePath, String folderName) throws IOException { |
||||||
|
Path source = Paths.get(sourcePath); |
||||||
|
Path targetFolder = Paths.get("downloads/" + folderName); |
||||||
|
Path target = targetFolder.resolve(source.getFileName()); |
||||||
|
|
||||||
|
// 创建目标文件夹(如果不存在)
|
||||||
|
if (!Files.exists(targetFolder)) { |
||||||
|
Files.createDirectories(targetFolder); |
||||||
|
} |
||||||
|
|
||||||
|
// 移动文件
|
||||||
|
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); |
||||||
|
} |
||||||
|
|
||||||
|
// 将多个文件夹打包成zip
|
||||||
|
public static void zipFolders(List<String> folderNames, String zipFilePath) throws IOException { |
||||||
|
try (FileOutputStream fos = new FileOutputStream(zipFilePath); |
||||||
|
ZipOutputStream zos = new ZipOutputStream(fos)) { |
||||||
|
|
||||||
|
for (String folderName : folderNames) { |
||||||
|
Path folderPath = Paths.get("downloads/" + folderName); |
||||||
|
zipFolder(folderPath, folderName, zos); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void doGet(HttpServletResponse response,String filePath,String zipName) throws IOException { |
||||||
|
// 从请求参数中获取文件路径(这里假设文件路径是通过请求参数传递的,实际应用中可能需要更安全的方式)
|
||||||
|
|
||||||
|
if (filePath == null || filePath.isEmpty()) { |
||||||
|
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "File path is required"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// 校验文件路径的合法性(这里只是简单示例,实际应用中需要更严格的校验)
|
||||||
|
File file = new File(filePath); |
||||||
|
if (!file.exists() || !file.isFile()) { |
||||||
|
response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// 获取MIME类型
|
||||||
|
String mimeType = "application/octet-stream"; |
||||||
|
|
||||||
|
// 设置响应内容类型
|
||||||
|
response.setContentType(mimeType); |
||||||
|
response.setContentLength((int) file.length()); |
||||||
|
|
||||||
|
// 设置Content-Disposition头以提示浏览器下载文件
|
||||||
|
String headerKey = "Content-Disposition"; |
||||||
|
String headerValue = String.format("attachment; filename=\"%s\"", URLEncoder.encode(zipName, "UTF-8")); |
||||||
|
response.setHeader(headerKey, headerValue); |
||||||
|
|
||||||
|
// 读取文件内容并写入响应输出流
|
||||||
|
try (FileInputStream inputStream = new FileInputStream(file); |
||||||
|
OutputStream outputStream = response.getOutputStream()) { |
||||||
|
|
||||||
|
byte[] buffer = new byte[1024]; |
||||||
|
int bytesRead; |
||||||
|
while ((bytesRead = inputStream.read(buffer)) != -1) { |
||||||
|
outputStream.write(buffer, 0, bytesRead); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static void zipFolder(Path folderPath, String parentFolderName, ZipOutputStream zos) throws IOException { |
||||||
|
for (File file : folderPath.toFile().listFiles()) { |
||||||
|
Path filePath = folderPath.resolve(file.getName()); |
||||||
|
if (Files.isDirectory(filePath)) { |
||||||
|
zipFolder(filePath, parentFolderName + "/" + file.getName(), zos); |
||||||
|
} else { |
||||||
|
try (FileInputStream fis = new FileInputStream(filePath.toFile())) { |
||||||
|
ZipEntry zipEntry = new ZipEntry(parentFolderName + "/" + file.getName()); |
||||||
|
zos.putNextEntry(zipEntry); |
||||||
|
byte[] buffer = new byte[1024]; |
||||||
|
int length; |
||||||
|
while ((length = fis.read(buffer)) > 0) { |
||||||
|
zos.write(buffer, 0, length); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue