【新增】下载文件,解压zip,获取文件路径
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package pc.exam.pp.module.judgement.controller.admin.Gcc;
|
||||
|
||||
public class GccController {
|
||||
}
|
@@ -1,4 +0,0 @@
|
||||
package pc.exam.pp.module.judgement.controller.admin;
|
||||
|
||||
public class GccController {
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
package pc.exam.pp.module.judgement.controller.admin.autoTools;
|
||||
|
||||
public class AutoToolsController {
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package pc.exam.pp.module.judgement.controller.admin.autoTools.vo;
|
||||
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class StudentUploadFileZipReqVo {
|
||||
|
||||
@Schema(description = "学号")
|
||||
private Long studentId;
|
||||
|
||||
@Schema(description = "试卷号")
|
||||
private String paperId;
|
||||
|
||||
@Schema(description = "上传文件路径")
|
||||
private String filePath;
|
||||
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package pc.exam.pp.module.judgement.service.auto_tools;
|
||||
|
||||
public interface AutoToolsService {
|
||||
|
||||
|
||||
// 文件自动上传
|
||||
|
||||
// Boolean autoFileTools(Long studentId, String paperId);
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
* @param fileUrl 文件url
|
||||
* @param filePath 要下载的文件路径
|
||||
* @return 下载后的文件path
|
||||
*/
|
||||
String downloadStudentFile(String fileUrl, String filePath);
|
||||
|
||||
//
|
||||
|
||||
/**
|
||||
* 解压文件
|
||||
* @param zipFilePath zip文件路径
|
||||
* @return 解压后的目录
|
||||
*/
|
||||
String unzipToNamedFolder(String zipFilePath);
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
package pc.exam.pp.module.judgement.service.auto_tools;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
@Service
|
||||
public class AutoToolsServiceImpl implements AutoToolsService{
|
||||
|
||||
@Override
|
||||
public String downloadStudentFile(String fileUrl, String filePath) {
|
||||
try {
|
||||
URL url = new URL(fileUrl);
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
String fileName = new File(url.getPath()).getName();
|
||||
File dir = new File(filePath);
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
|
||||
File saveFile = new File(dir, fileName);
|
||||
|
||||
try (InputStream in = connection.getInputStream();
|
||||
FileOutputStream out = new FileOutputStream(saveFile)) {
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
System.out.println("✅ 下载成功: " + saveFile.getAbsolutePath());
|
||||
return saveFile.getAbsolutePath();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("❌ 下载失败: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String unzipToNamedFolder(String zipFilePath) {
|
||||
File zipFile = new File(zipFilePath);
|
||||
|
||||
if (!zipFile.exists() || !zipFile.getName().toLowerCase().endsWith(".zip")) {
|
||||
System.err.println("❌ 无效 zip 文件: " + zipFilePath);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 提取 zip 文件名(无扩展)
|
||||
String fileNameNoExt = zipFile.getName().replaceAll("(?i)\\.zip$", "");
|
||||
// 自动拼接系统适配路径
|
||||
File extractDir = new File(zipFile.getParentFile(), fileNameNoExt);
|
||||
|
||||
if (!extractDir.exists()) {
|
||||
extractDir.mkdirs();
|
||||
}
|
||||
|
||||
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
|
||||
ZipEntry entry;
|
||||
while ((entry = zis.getNextEntry()) != null) {
|
||||
File outFile = new File(extractDir, entry.getName());
|
||||
|
||||
// 防止 Zip 路径穿越攻击
|
||||
if (!outFile.getCanonicalPath().startsWith(extractDir.getCanonicalPath())) {
|
||||
throw new IOException("非法路径: " + entry.getName());
|
||||
}
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
outFile.mkdirs();
|
||||
} else {
|
||||
File parent = outFile.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(outFile)) {
|
||||
byte[] buffer = new byte[4096];
|
||||
int len;
|
||||
while ((len = zis.read(buffer)) > 0) {
|
||||
fos.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
zis.closeEntry();
|
||||
}
|
||||
System.out.println("✅ 解压完成,目录:" + extractDir.getAbsolutePath());
|
||||
return extractDir.getAbsolutePath();
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("❌ 解压出错: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user