|
@@ -1,31 +1,45 @@
|
|
|
package com.taais.web.controller.common;
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import com.taais.common.core.config.TaaisConfig;
|
|
|
import com.taais.common.core.constant.Constants;
|
|
|
import com.taais.common.core.core.domain.CommonResult;
|
|
|
+import com.taais.common.core.utils.MapstructUtils;
|
|
|
import com.taais.common.core.utils.StringUtils;
|
|
|
import com.taais.common.core.utils.file.FileUploadUtils;
|
|
|
import com.taais.common.core.utils.file.FileUtils;
|
|
|
+import com.taais.common.core.utils.file.UnPackedUtil;
|
|
|
+import com.taais.common.excel.utils.ExcelUtil;
|
|
|
+import com.taais.demo.domain.Data;
|
|
|
+import com.taais.demo.domain.vo.DataVo;
|
|
|
+import com.taais.demo.service.IDataService;
|
|
|
import com.taais.system.config.ServerConfig;
|
|
|
import com.taais.system.domain.vo.SysOssUploadVo;
|
|
|
import com.taais.system.domain.vo.SysOssVo;
|
|
|
import com.taais.system.service.ISysOssService;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import net.lingala.zip4j.model.FileHeader;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.MediaType;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.*;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+import static com.taais.common.security.utils.LoginHelper.getLoginUser;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 通用请求处理
|
|
@@ -41,6 +55,9 @@ public class CommonController {
|
|
|
private ServerConfig serverConfig;
|
|
|
private final ISysOssService sysOssService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IDataService dataService;
|
|
|
+
|
|
|
public CommonController(ISysOssService sysOssService) {
|
|
|
this.sysOssService = sysOssService;
|
|
|
}
|
|
@@ -76,18 +93,19 @@ public class CommonController {
|
|
|
@PostMapping("/upload")
|
|
|
public CommonResult<SysOssUploadVo> uploadFile(MultipartFile file) throws Exception {
|
|
|
try {
|
|
|
-
|
|
|
// 上传文件路径
|
|
|
String filePath = TaaisConfig.getUploadPath();
|
|
|
+
|
|
|
// 上传并返回新文件名称
|
|
|
+
|
|
|
String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
String url = serverConfig.getUrl() + fileName;
|
|
|
String originalFilename = file.getOriginalFilename();
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- map.put("url", url);
|
|
|
- map.put("fileName", fileName);
|
|
|
- map.put("newFileName", FileUtils.getName(fileName));
|
|
|
- map.put("originalFilename", originalFilename);
|
|
|
+// Map<String, Object> map = new HashMap<>();
|
|
|
+// map.put("url", url);
|
|
|
+// map.put("fileName", fileName);
|
|
|
+// map.put("newFileName", FileUtils.getName(fileName));
|
|
|
+// map.put("originalFilename", originalFilename);
|
|
|
assert originalFilename != null;
|
|
|
String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
|
|
|
// 信息保存在文件管理
|
|
@@ -102,6 +120,105 @@ public class CommonController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @PostMapping("/zip/upload")
|
|
|
+ public CommonResult<SysOssUploadVo> uploadZipFile(@RequestParam("file") MultipartFile file, @RequestParam("excelFile") MultipartFile excelFile) throws Exception {
|
|
|
+// try {
|
|
|
+
|
|
|
+
|
|
|
+ List<DataVo> userList = ExcelUtil.importExcel(excelFile.getInputStream(),DataVo.class);
|
|
|
+ Map<String, Data> pic_name2obj = new HashMap<>();
|
|
|
+ //为插入数据库做准备
|
|
|
+ for(DataVo dataVo:userList){
|
|
|
+ pic_name2obj.put(dataVo.getName(), MapstructUtils.convert(dataVo, Data.class));
|
|
|
+ }
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ assert originalFilename != null;
|
|
|
+ String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
|
|
|
+
|
|
|
+
|
|
|
+ // 解压
|
|
|
+ String basedir = TaaisConfig.getUploadPath();
|
|
|
+ File ZipOrRar_temp = FileUploadUtils.getAbsoluteFile(basedir,FileUploadUtils.extractFilename(file));// 解压目录
|
|
|
+ String dest_zip = ZipOrRar_temp.getAbsolutePath();
|
|
|
+
|
|
|
+ file.transferTo(Paths.get(dest_zip));
|
|
|
+ String dest = ZipOrRar_temp.getParent();
|
|
|
+
|
|
|
+ List<File> extractedFileList = new ArrayList<File>();
|
|
|
+ if(suffix.equals(".zip")){
|
|
|
+ List<FileHeader> fileheaders = UnPackedUtil.unPackZip(ZipOrRar_temp,dest);
|
|
|
+ //解压后的目录
|
|
|
+ String dir_name = fileheaders.get(0).getFileName();
|
|
|
+ dest = dest + '/' + dir_name.substring(0,dir_name.length()-1);
|
|
|
+ for(FileHeader fileHeader : fileheaders) {
|
|
|
+
|
|
|
+ if (!fileHeader.isDirectory()) {
|
|
|
+ String fileHeaderName = fileHeader.getFileName();
|
|
|
+ String fileHeader_suffix = StringUtils.substring(fileHeaderName, fileHeaderName.lastIndexOf("."), fileHeaderName.length());
|
|
|
+ if(!fileHeader_suffix.equals(".jpg")&&!fileHeader_suffix.equals(".jpeg")
|
|
|
+ &&!fileHeader_suffix.equals(".png")){
|
|
|
+ continue;
|
|
|
+
|
|
|
+// for(File efile:extractedFileList){
|
|
|
+// efile.delete();
|
|
|
+//
|
|
|
+// }
|
|
|
+// return CommonResult.fail("压缩文件里有文件并不是图片!");
|
|
|
+ }
|
|
|
+ extractedFileList.add(new File(dest,fileHeader.getFileName()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }else if(suffix.equals(".rar")){
|
|
|
+ List<com.github.junrar.rarfile.FileHeader> fileheaders = UnPackedUtil.unPackRar(ZipOrRar_temp,dest);
|
|
|
+
|
|
|
+ //解压后的目录
|
|
|
+ String dir_name = fileheaders.get(0).getFileName();
|
|
|
+ dest = dest + '/' + dir_name.substring(0,dir_name.length()-1);
|
|
|
+ for(com.github.junrar.rarfile.FileHeader fileHeader : fileheaders) {
|
|
|
+
|
|
|
+ if (!fileHeader.isDirectory()) {
|
|
|
+ String fileHeaderName = fileHeader.getFileName();
|
|
|
+ String fileHeader_suffix = StringUtils.substring(fileHeaderName, fileHeaderName.lastIndexOf("."), fileHeaderName.length());
|
|
|
+ if(!fileHeader_suffix.equals(".jpg")&&!fileHeader_suffix.equals(".jpeg")
|
|
|
+ &&!fileHeader_suffix.equals(".png")){
|
|
|
+ continue;
|
|
|
+
|
|
|
+// for(File efile:extractedFileList){
|
|
|
+// efile.delete();
|
|
|
+//
|
|
|
+// }
|
|
|
+// return CommonResult.fail("压缩文件里有文件并不是图片!");
|
|
|
+ }
|
|
|
+ extractedFileList.add(new File(dest,fileHeader.getFileName()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for(File efile:extractedFileList){
|
|
|
+ String fileName = FileUploadUtils.getPathFileName(dest,efile.getName());
|
|
|
+ String url = serverConfig.getUrl() + fileName;
|
|
|
+ url = url.replaceAll("\\\\", "/");
|
|
|
+ Data mydata = pic_name2obj.get(efile.getName());
|
|
|
+
|
|
|
+ // TODO: 将导入的excel表和url一起插入数据库,图片名称对应
|
|
|
+ mydata.setUrl(url);
|
|
|
+ dataService.save(mydata);
|
|
|
+ }
|
|
|
+ //删除压缩文件
|
|
|
+ FileUtils.deleteFile(dest_zip);
|
|
|
+
|
|
|
+
|
|
|
+ return CommonResult.success("数据集上传成功!");
|
|
|
+// } catch (Exception e) {
|
|
|
+// return CommonResult.fail(e.getMessage());
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 通用上传请求(多个)
|
|
|
*/
|
|
@@ -157,4 +274,62 @@ public class CommonController {
|
|
|
log.error("下载文件失败", e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件打包下载
|
|
|
+ * @param response
|
|
|
+ * @param files
|
|
|
+ */
|
|
|
+ @GetMapping ("/zip")
|
|
|
+ public void downloadZip(HttpServletResponse response, String files){
|
|
|
+ //下载压缩文件的名称
|
|
|
+ String zipname= getLoginUser().getUsername();
|
|
|
+
|
|
|
+ String[] filesPaths = Convert.toStrArray(files);
|
|
|
+
|
|
|
+ String fileName = zipname + ".zip";
|
|
|
+ response.setContentType("application/zip");
|
|
|
+ response.setHeader("content-disposition", "attachment;filename=" + fileName);
|
|
|
+
|
|
|
+ ZipOutputStream zos = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try{
|
|
|
+ zos = new ZipOutputStream(response.getOutputStream());
|
|
|
+ byte[] buf = new byte[8192];
|
|
|
+ int len;
|
|
|
+ for (int i = 0; i < filesPaths.length; i++) {
|
|
|
+ File file = new File(filesPaths[i]);
|
|
|
+ if (!file.isFile()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ZipEntry ze = new ZipEntry(file.getName());
|
|
|
+ zos.putNextEntry(ze);
|
|
|
+ bis = new BufferedInputStream(new FileInputStream(file));
|
|
|
+ while ((len = bis.read(buf)) > 0) {
|
|
|
+ zos.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ zos.closeEntry();
|
|
|
+ }
|
|
|
+ zos.closeEntry();
|
|
|
+ }catch(Exception ex){
|
|
|
+ ex.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if(bis != null){
|
|
|
+ try{
|
|
|
+ bis.close();
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(zos != null){
|
|
|
+ try{
|
|
|
+ zos.close();
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|