From eab5e6bba1e65a43e7067c8ad1605053f31b0de8 Mon Sep 17 00:00:00 2001 From: MAC <1162714483@qq.com> Date: Mon, 11 Apr 2022 23:06:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(=E6=90=9C=E7=B4=A2=E4=BF=AE=E5=A4=8D):?= =?UTF-8?q?=20=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 14 +- .../file/api/IElasticSearchService.java | 20 +-- .../file/api/IFiletransferService.java | 3 + .../file/component/FileDealComp.java | 13 +- .../file/config/es/ElasticSearchConfig.java | 22 +++ .../qiwenshare/file/config/es/FileSearch.java | 29 +-- .../file/controller/FileController.java | 109 ++++++------ .../controller/FiletransferController.java | 27 ++- .../file/controller/TaskController.java | 10 +- .../file/dto/file/BatchDownloadFileDTO.java | 12 ++ .../file/dto/file/SearchFileDTO.java | 4 - .../file/service/FiletransferService.java | 165 +++++++++--------- .../qiwenshare/file/vo/file/SearchFileVO.java | 24 +++ 13 files changed, 256 insertions(+), 196 deletions(-) create mode 100644 src/main/java/com/qiwenshare/file/config/es/ElasticSearchConfig.java create mode 100644 src/main/java/com/qiwenshare/file/dto/file/BatchDownloadFileDTO.java create mode 100644 src/main/java/com/qiwenshare/file/vo/file/SearchFileVO.java diff --git a/pom.xml b/pom.xml index 3a8be54..73733d1 100644 --- a/pom.xml +++ b/pom.xml @@ -98,8 +98,18 @@ - org.springframework.boot - spring-boot-starter-data-elasticsearch + co.elastic.clients + elasticsearch-java + 8.1.2 + + + com.fasterxml.jackson.core + jackson-databind + + + jakarta.json + jakarta.json-api + 2.0.1 diff --git a/src/main/java/com/qiwenshare/file/api/IElasticSearchService.java b/src/main/java/com/qiwenshare/file/api/IElasticSearchService.java index 473c3ef..a425f55 100644 --- a/src/main/java/com/qiwenshare/file/api/IElasticSearchService.java +++ b/src/main/java/com/qiwenshare/file/api/IElasticSearchService.java @@ -1,10 +1,10 @@ -package com.qiwenshare.file.api; - -import com.qiwenshare.file.config.es.FileSearch; -import org.springframework.context.annotation.Lazy; -import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; - -@Lazy -public interface IElasticSearchService extends ElasticsearchRepository { - -} \ No newline at end of file +//package com.qiwenshare.file.api; +// +//import com.qiwenshare.file.config.es.FileSearch; +//import org.springframework.context.annotation.Lazy; +//import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; +// +//@Lazy +//public interface IElasticSearchService extends ElasticsearchRepository { +// +//} \ No newline at end of file diff --git a/src/main/java/com/qiwenshare/file/api/IFiletransferService.java b/src/main/java/com/qiwenshare/file/api/IFiletransferService.java index 030b397..8315da7 100644 --- a/src/main/java/com/qiwenshare/file/api/IFiletransferService.java +++ b/src/main/java/com/qiwenshare/file/api/IFiletransferService.java @@ -1,6 +1,7 @@ package com.qiwenshare.file.api; import com.qiwenshare.file.domain.FileBean; +import com.qiwenshare.file.domain.UserFile; import com.qiwenshare.file.dto.file.DownloadFileDTO; import com.qiwenshare.file.dto.file.PreviewDTO; import com.qiwenshare.file.dto.file.UploadFileDTO; @@ -8,6 +9,7 @@ import com.qiwenshare.file.vo.file.UploadFileVo; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.List; public interface IFiletransferService { @@ -16,6 +18,7 @@ public interface IFiletransferService { void uploadFile(HttpServletRequest request, UploadFileDTO UploadFileDto, Long userId); void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO); + void downloadUserFileList(HttpServletResponse httpServletResponse, String filePath, String fileName, List userFileList); void previewFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO); void previewPictureFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO); void deleteFile(FileBean fileBean); diff --git a/src/main/java/com/qiwenshare/file/component/FileDealComp.java b/src/main/java/com/qiwenshare/file/component/FileDealComp.java index b419caa..94270af 100644 --- a/src/main/java/com/qiwenshare/file/component/FileDealComp.java +++ b/src/main/java/com/qiwenshare/file/component/FileDealComp.java @@ -1,6 +1,7 @@ package com.qiwenshare.file.component; import cn.hutool.core.bean.BeanUtil; +import co.elastic.clients.elasticsearch.ElasticsearchClient; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.qiwenshare.common.constant.FileConstant; @@ -60,9 +61,11 @@ public class FileDealComp { @Resource UFOPFactory ufopFactory; +// @Autowired +// @Lazy +// private IElasticSearchService elasticSearchService; @Autowired - @Lazy - private IElasticSearchService elasticSearchService; + private ElasticsearchClient elasticsearchClient; public static Executor exec = Executors.newFixedThreadPool(10); @@ -292,7 +295,7 @@ public class FileDealComp { // fileSearch.setContent(content); // // } - elasticSearchService.save(fileSearch); + elasticsearchClient.index(i -> i.index("filesearch").id(String.valueOf(fileSearch.getUserFileId())).document(fileSearch)); } } catch (Exception e) { log.debug("ES更新操作失败,请检查配置"); @@ -303,7 +306,9 @@ public class FileDealComp { public void deleteESByUserFileId(Long userFileId) { exec.execute(()->{ try { - elasticSearchService.deleteById(userFileId); + elasticsearchClient.delete(d -> d + .index("filesearch") + .id(String.valueOf(userFileId))); } catch (Exception e) { log.debug("ES删除操作失败,请检查配置"); } diff --git a/src/main/java/com/qiwenshare/file/config/es/ElasticSearchConfig.java b/src/main/java/com/qiwenshare/file/config/es/ElasticSearchConfig.java new file mode 100644 index 0000000..92aef34 --- /dev/null +++ b/src/main/java/com/qiwenshare/file/config/es/ElasticSearchConfig.java @@ -0,0 +1,22 @@ +package com.qiwenshare.file.config.es; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.json.jackson.JacksonJsonpMapper; +import co.elastic.clients.transport.ElasticsearchTransport; +import co.elastic.clients.transport.rest_client.RestClientTransport; +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ElasticSearchConfig { + + //注入IOC容器 + @Bean + public ElasticsearchClient elasticsearchClient(){ + RestClient client = RestClient.builder(new HttpHost("localhost", 9200,"http")).build(); + ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper()); + return new ElasticsearchClient(transport); + } +} \ No newline at end of file diff --git a/src/main/java/com/qiwenshare/file/config/es/FileSearch.java b/src/main/java/com/qiwenshare/file/config/es/FileSearch.java index 7048989..e8b4467 100644 --- a/src/main/java/com/qiwenshare/file/config/es/FileSearch.java +++ b/src/main/java/com/qiwenshare/file/config/es/FileSearch.java @@ -1,51 +1,24 @@ package com.qiwenshare.file.config.es; import lombok.Data; -import org.springframework.data.annotation.Id; -import org.springframework.data.elasticsearch.annotations.Document; -import org.springframework.data.elasticsearch.annotations.Field; -import org.springframework.data.elasticsearch.annotations.FieldType; @Data -@Document(indexName = "filesearch") public class FileSearch { - @Id + private String indexName; private Long userFileId; - @Field(type = FieldType.Long) private Long fileId; - @Field(type = FieldType.Text) private String fileName; - @Field(type = FieldType.Text) private String content; - @Field(type = FieldType.Keyword) - private String timeStampName; - @Field(type = FieldType.Keyword) private String fileUrl; - @Field(type = FieldType.Long) private Long fileSize; - @Field(type = FieldType.Integer) - @Deprecated - private Integer isOSS; - @Field(type = FieldType.Integer) private Integer storageType; - @Field(type = FieldType.Integer) - private Integer pointCount; - @Field(type = FieldType.Keyword) private String identifier; - @Field(type = FieldType.Long) private Long userId; - @Field(type = FieldType.Keyword) private String filePath; - @Field(type = FieldType.Keyword) private String extendName; - @Field(type = FieldType.Integer) private Integer isDir; - @Field(type = FieldType.Keyword) private String uploadTime; - @Field(type = FieldType.Integer) private Integer deleteFlag; - @Field(type = FieldType.Keyword) private String deleteTime; - @Field(type = FieldType.Keyword) private String deleteBatchNum; } diff --git a/src/main/java/com/qiwenshare/file/controller/FileController.java b/src/main/java/com/qiwenshare/file/controller/FileController.java index 81cceec..00f0dce 100644 --- a/src/main/java/com/qiwenshare/file/controller/FileController.java +++ b/src/main/java/com/qiwenshare/file/controller/FileController.java @@ -1,5 +1,11 @@ package com.qiwenshare.file.controller; +import cn.hutool.core.bean.BeanUtil; +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.elasticsearch._types.SortOrder; +import co.elastic.clients.elasticsearch.core.SearchResponse; +import co.elastic.clients.elasticsearch.core.search.HighlighterEncoder; +import co.elastic.clients.elasticsearch.core.search.Hit; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -19,6 +25,7 @@ import com.qiwenshare.file.domain.UserFile; import com.qiwenshare.file.dto.file.*; import com.qiwenshare.file.util.TreeNode; import com.qiwenshare.file.vo.file.FileListVo; +import com.qiwenshare.file.vo.file.SearchFileVO; import com.qiwenshare.ufop.constant.UFOPConstant; import com.qiwenshare.ufop.factory.UFOPFactory; import io.swagger.v3.oas.annotations.Operation; @@ -26,18 +33,9 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang3.StringUtils; import org.eclipse.jetty.util.StringUtil; -import org.elasticsearch.index.query.DisMaxQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Sort; -import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; -import org.springframework.data.elasticsearch.core.SearchHits; -import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; -import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -59,12 +57,12 @@ public class FileController { @Resource IUserFileService userFileService; - @Autowired - private ElasticsearchRestTemplate elasticsearchRestTemplate; @Resource FileDealComp fileDealComp; @Resource UFOPFactory ufopFactory; + @Autowired + private ElasticsearchClient elasticsearchClient; public static final String CURRENT_MODULE = "文件接口"; @@ -100,59 +98,56 @@ public class FileController { @GetMapping(value = "/search") @MyLog(operation = "文件搜索", module = CURRENT_MODULE) @ResponseBody - public RestResult> searchFile(SearchFileDTO searchFileDTO) { + public RestResult> searchFile(SearchFileDTO searchFileDTO) { JwtUser sessionUserBean = SessionUtil.getSession(); - NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); - HighlightBuilder.Field allHighLight = new HighlightBuilder.Field("*").preTags("") - .postTags(""); - queryBuilder.withHighlightFields(allHighLight); - - //设置分页 int currentPage = (int)searchFileDTO.getCurrentPage() - 1; int pageCount = (int)(searchFileDTO.getPageCount() == 0 ? 10 : searchFileDTO.getPageCount()); - String order = searchFileDTO.getOrder(); - Sort.Direction direction = null; - if (searchFileDTO.getDirection() == null) { - direction = Sort.Direction.DESC; - } else if ("asc".equals(searchFileDTO.getDirection())) { - direction = Sort.Direction.ASC; - } else if ("desc".equals(searchFileDTO.getDirection())) { - direction = Sort.Direction.DESC; - } else { - direction = Sort.Direction.DESC; + + SearchResponse search = null; + try { + search = elasticsearchClient.search(s -> s + .index("filesearch") + .query(_1 -> _1 + .bool(_2 -> _2 + .must(_3 -> _3 + .bool(_4 -> _4 + .should(_5 -> _5 + .match(_6 -> _6 + .field("fileName") + .query(searchFileDTO.getFileName()))) + .should(_5 -> _5 + .wildcard(_6 -> _6 + .field("fileName") + .wildcard("*" + searchFileDTO.getFileName() + "*"))))) + .must(_3 -> _3 + .term(_4 -> _4 + .field("userId") + .value(sessionUserBean.getUserId()))) + )) + .from(currentPage) + .size(pageCount) + .highlight(h -> h + .fields("fileName", f -> f.type("plain") + .preTags("").postTags("")) + .encoder(HighlighterEncoder.Html)) + , + FileSearch.class); + } catch (IOException e) { + e.printStackTrace(); } - if (order == null) { - queryBuilder.withPageable(PageRequest.of(currentPage, pageCount)); - } else { - queryBuilder.withPageable(PageRequest.of(currentPage, pageCount, Sort.by(direction, order))); + + List searchFileVOList = new ArrayList<>(); + for (Hit hit : search.hits().hits()) { + SearchFileVO searchFileVO = new SearchFileVO(); + BeanUtil.copyProperties(hit.source(), searchFileVO); + searchFileVO.setHighLight(hit.highlight()); + searchFileVOList.add(searchFileVO); } - DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery(); - - QueryBuilder q1 = QueryBuilders.boolQuery() - .must(QueryBuilders.multiMatchQuery(searchFileDTO.getFileName(), "fileName", "content")) - .must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId())).boost(1f); //分词 - - QueryBuilder q2 = QueryBuilders.boolQuery() - .must(QueryBuilders.wildcardQuery("fileName", "*" + searchFileDTO.getFileName() + "*")) - .must(QueryBuilders.wildcardQuery("content", "*" + searchFileDTO.getFileName() + "*")) - .must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId())).boost(2f); //模糊匹配 - - disMaxQueryBuilder.add(q1); - disMaxQueryBuilder.add(q2); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(disMaxQueryBuilder).withHighlightFields(allHighLight).build(); -// -// queryBuilder.withQuery(QueryBuilders.boolQuery() -//// .must(QueryBuilders.matchQuery("fileName", searchFileDTO.getFileName())) -// .must(QueryBuilders.multiMatchQuery(searchFileDTO.getFileName(),"fileName", "content")) -// .must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId())) -// ).withQuery(QueryBuilders.wildcardQuery("fileName", "*" + searchFileDTO.getFileName() + "*")); - SearchHits search = elasticsearchRestTemplate.search(searchQuery, FileSearch.class); - - return RestResult.success().data(search); + return RestResult.success().data(searchFileVOList); } + @Operation(summary = "文件重命名", description = "文件重命名", tags = {"file"}) @RequestMapping(value = "/renamefile", method = RequestMethod.POST) @MyLog(operation = "文件重命名", module = CURRENT_MODULE) diff --git a/src/main/java/com/qiwenshare/file/controller/FiletransferController.java b/src/main/java/com/qiwenshare/file/controller/FiletransferController.java index fec29e3..4f7d8de 100644 --- a/src/main/java/com/qiwenshare/file/controller/FiletransferController.java +++ b/src/main/java/com/qiwenshare/file/controller/FiletransferController.java @@ -1,17 +1,18 @@ package com.qiwenshare.file.controller; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.alibaba.fastjson.JSON; import com.qiwenshare.common.anno.MyLog; import com.qiwenshare.common.result.RestResult; +import com.qiwenshare.common.util.DateUtil; import com.qiwenshare.common.util.MimeUtils; import com.qiwenshare.common.util.security.JwtUser; import com.qiwenshare.common.util.security.SessionUtil; import com.qiwenshare.file.api.*; import com.qiwenshare.file.component.FileDealComp; import com.qiwenshare.file.domain.FileBean; -import com.qiwenshare.file.domain.Image; import com.qiwenshare.file.domain.StorageBean; import com.qiwenshare.file.domain.UserFile; +import com.qiwenshare.file.dto.file.BatchDeleteFileDTO; import com.qiwenshare.file.dto.file.DownloadFileDTO; import com.qiwenshare.file.dto.file.PreviewDTO; import com.qiwenshare.file.dto.file.UploadFileDTO; @@ -25,17 +26,14 @@ import com.qiwenshare.ufop.util.UFOPUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; - import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import javax.imageio.ImageIO; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -147,6 +145,25 @@ public class FiletransferController { filetransferService.downloadFile(httpServletResponse, downloadFileDTO); } + @Operation(summary = "批量下载文件", description = "批量下载文件", tags = {"filetransfer"}) + @RequestMapping(value = "/batchdeletefile", method = RequestMethod.POST) + @MyLog(operation = "批量删除文件", module = CURRENT_MODULE) + @ResponseBody + public RestResult batchDownloadFile(HttpServletResponse httpServletResponse, @RequestBody BatchDeleteFileDTO batchDeleteFileDto) { + + List userFiles = JSON.parseArray(batchDeleteFileDto.getFiles(), UserFile.class); + if (userFiles == null || userFiles.isEmpty()) { + return RestResult.fail().message("文件列表为空!"); + } + UserFile userFile = userFileService.getById(userFiles.get(0).getUserFileId()); + httpServletResponse.setContentType("application/force-download");// 设置强制下载不打开 + String fileName = DateUtil.getCurrentTime(); + httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名 + filetransferService.downloadUserFileList(httpServletResponse, userFile.getFilePath(), fileName, userFiles); + + return RestResult.success().message("批量下载文件成功"); + } + @Operation(summary="预览文件", description="用于文件预览", tags = {"filetransfer"}) @GetMapping("/preview") public void preview(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, PreviewDTO previewDTO) throws IOException { diff --git a/src/main/java/com/qiwenshare/file/controller/TaskController.java b/src/main/java/com/qiwenshare/file/controller/TaskController.java index a0353e9..6bdff3f 100644 --- a/src/main/java/com/qiwenshare/file/controller/TaskController.java +++ b/src/main/java/com/qiwenshare/file/controller/TaskController.java @@ -1,12 +1,11 @@ package com.qiwenshare.file.controller; -import com.qiwenshare.file.api.IElasticSearchService; +import co.elastic.clients.elasticsearch.ElasticsearchClient; import com.qiwenshare.file.component.FileDealComp; import com.qiwenshare.file.domain.UserFile; import com.qiwenshare.file.service.UserFileService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Controller; @@ -19,18 +18,17 @@ public class TaskController { @Resource UserFileService userFileService; - @Autowired - @Lazy - private IElasticSearchService elasticSearchService; @Resource FileDealComp fileDealComp; + @Autowired + private ElasticsearchClient elasticsearchClient; @Scheduled(fixedRate = 1000 * 60 * 60 * 24) public void updateElasticSearch() { try { - elasticSearchService.deleteAll(); + elasticsearchClient.delete(d -> d.index("filesearch")); } catch (Exception e) { log.debug("删除ES失败:" + e); } diff --git a/src/main/java/com/qiwenshare/file/dto/file/BatchDownloadFileDTO.java b/src/main/java/com/qiwenshare/file/dto/file/BatchDownloadFileDTO.java new file mode 100644 index 0000000..6af80f9 --- /dev/null +++ b/src/main/java/com/qiwenshare/file/dto/file/BatchDownloadFileDTO.java @@ -0,0 +1,12 @@ +package com.qiwenshare.file.dto.file; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +@Schema(name = "批量下载文件DTO",required = true) +public class BatchDownloadFileDTO { + @Schema(description="文件集合", required = true) + private String files; + +} diff --git a/src/main/java/com/qiwenshare/file/dto/file/SearchFileDTO.java b/src/main/java/com/qiwenshare/file/dto/file/SearchFileDTO.java index 7260336..dff148b 100644 --- a/src/main/java/com/qiwenshare/file/dto/file/SearchFileDTO.java +++ b/src/main/java/com/qiwenshare/file/dto/file/SearchFileDTO.java @@ -11,8 +11,4 @@ public class SearchFileDTO { private long currentPage; @Schema(description="每页数量", required=true) private long pageCount; - @Schema(description="排序字段(可排序字段:fileName, fileSize, extendName, uploadTime)", required=false) - private String order; - @Schema(description="方向(升序:asc, 降序:desc)", required=false) - private String direction; } diff --git a/src/main/java/com/qiwenshare/file/service/FiletransferService.java b/src/main/java/com/qiwenshare/file/service/FiletransferService.java index 8a48fbe..e8c2e4c 100644 --- a/src/main/java/com/qiwenshare/file/service/FiletransferService.java +++ b/src/main/java/com/qiwenshare/file/service/FiletransferService.java @@ -290,92 +290,97 @@ public class FiletransferService implements IFiletransferService { .eq(UserFile::getDeleteFlag, 0); List userFileList = userFileMapper.selectList(lambdaQueryWrapper); - String staticPath = UFOPUtils.getStaticPath(); - String tempPath = staticPath + "temp" + File.separator; - File tempDirFile = new File(tempPath); - if (!tempDirFile.exists()) { - tempDirFile.mkdirs(); - } + downloadUserFileList(httpServletResponse, userFile.getFilePath(), userFile.getFileName(), userFileList); + } + } - FileOutputStream f = null; + @Override + public void downloadUserFileList(HttpServletResponse httpServletResponse, String filePath, String fileName, List userFileList) { + String staticPath = UFOPUtils.getStaticPath(); + String tempPath = staticPath + "temp" + File.separator; + File tempDirFile = new File(tempPath); + if (!tempDirFile.exists()) { + tempDirFile.mkdirs(); + } + + FileOutputStream f = null; + try { + f = new FileOutputStream(tempPath + fileName + ".zip"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); + ZipOutputStream zos = new ZipOutputStream(csum); + BufferedOutputStream out = new BufferedOutputStream(zos); + + try { + for (UserFile userFile1 : userFileList) { + FileBean fileBean = fileMapper.selectById(userFile1.getFileId()); + Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType()); + if (downloader == null) { + log.error("下载失败,文件存储类型不支持下载,storageType:{}", fileBean.getStorageType()); + throw new UploadException("下载失败"); + } + DownloadFile downloadFile = new DownloadFile(); + downloadFile.setFileUrl(fileBean.getFileUrl()); + downloadFile.setFileSize(fileBean.getFileSize()); + InputStream inputStream = downloader.getInputStream(downloadFile); + BufferedInputStream bis = new BufferedInputStream(inputStream); + try { + zos.putNextEntry(new ZipEntry(userFile1.getFilePath().replace(filePath, "/") + userFile1.getFileName() + "." + userFile1.getExtendName())); + + byte[] buffer = new byte[1024]; + int i = bis.read(buffer); + while (i != -1) { + out.write(buffer, 0, i); + i = bis.read(buffer); + } + } catch (IOException e) { + log.error("" + e); + e.printStackTrace(); + } finally { + try { + bis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + out.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } catch (Exception e) { + log.error("压缩过程中出现异常:"+ e); + } finally { try { - f = new FileOutputStream(tempPath + userFile.getFileName() + ".zip"); - } catch (FileNotFoundException e) { + out.close(); + } catch (IOException e) { e.printStackTrace(); } - CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); - ZipOutputStream zos = new ZipOutputStream(csum); - BufferedOutputStream out = new BufferedOutputStream(zos); - - try { - for (UserFile userFile1 : userFileList) { - FileBean fileBean = fileMapper.selectById(userFile1.getFileId()); - Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType()); - if (downloader == null) { - log.error("下载失败,文件存储类型不支持下载,storageType:{}", fileBean.getStorageType()); - throw new UploadException("下载失败"); - } - DownloadFile downloadFile = new DownloadFile(); - downloadFile.setFileUrl(fileBean.getFileUrl()); - downloadFile.setFileSize(fileBean.getFileSize()); - InputStream inputStream = downloader.getInputStream(downloadFile); - BufferedInputStream bis = new BufferedInputStream(inputStream); - try { - zos.putNextEntry(new ZipEntry(userFile1.getFilePath().replace(userFile.getFilePath(), "/") + userFile1.getFileName() + "." + userFile1.getExtendName())); - - byte[] buffer = new byte[1024]; - int i = bis.read(buffer); - while (i != -1) { - out.write(buffer, 0, i); - i = bis.read(buffer); - } - } catch (IOException e) { - log.error("" + e); - e.printStackTrace(); - } finally { - try { - bis.close(); - } catch (IOException e) { - e.printStackTrace(); - } - try { - out.flush(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } catch (Exception e) { - log.error("压缩过程中出现异常:"+ e); - } finally { - try { - out.close(); - } catch (IOException e) { - e.printStackTrace(); - } + } + String zipPath = ""; + try { + Downloader downloader = ufopFactory.getDownloader(StorageTypeEnum.LOCAL.getCode()); + DownloadFile downloadFile = new DownloadFile(); + downloadFile.setFileUrl("temp" + File.separator + fileName + ".zip"); + File tempFile = new File(UFOPUtils.getStaticPath() + downloadFile.getFileUrl()); + httpServletResponse.setContentLengthLong(tempFile.length()); + downloader.download(httpServletResponse, downloadFile); + zipPath = UFOPUtils.getStaticPath() + "temp" + File.separator + fileName + ".zip"; + } catch (Exception e) { + //org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer + if (e.getMessage().contains("ClientAbortException")) { + //该异常忽略不做处理 + } else { + log.error("下传zip文件出现异常:{}", e.getMessage()); } - String zipPath = ""; - try { - Downloader downloader = ufopFactory.getDownloader(StorageTypeEnum.LOCAL.getCode()); - DownloadFile downloadFile = new DownloadFile(); - downloadFile.setFileUrl("temp" + File.separator + userFile.getFileName() + ".zip"); - File tempFile = new File(UFOPUtils.getStaticPath() + downloadFile.getFileUrl()); - httpServletResponse.setContentLengthLong(tempFile.length()); - downloader.download(httpServletResponse, downloadFile); - zipPath = UFOPUtils.getStaticPath() + "temp" + File.separator + userFile.getFileName() + ".zip"; - } catch (Exception e) { - //org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer - if (e.getMessage().contains("ClientAbortException")) { - //该异常忽略不做处理 - } else { - log.error("下传zip文件出现异常:{}", e.getMessage()); - } - } finally { - File file = new File(zipPath); - if (file.exists()) { - file.delete(); - } + } finally { + File file = new File(zipPath); + if (file.exists()) { + file.delete(); } } } diff --git a/src/main/java/com/qiwenshare/file/vo/file/SearchFileVO.java b/src/main/java/com/qiwenshare/file/vo/file/SearchFileVO.java new file mode 100644 index 0000000..ef4ce18 --- /dev/null +++ b/src/main/java/com/qiwenshare/file/vo/file/SearchFileVO.java @@ -0,0 +1,24 @@ +package com.qiwenshare.file.vo.file; + +import lombok.Data; + +import java.util.List; +import java.util.Map; + +/** + * @author MAC + * @version 1.0 + * @description: TODO + * @date 2022/4/10 11:04 + */ +@Data +public class SearchFileVO { + private Long userFileId; + private String fileName; + private String filePath; + private String extendName; + private Long fileSize; + private String fileUrl; + private Map> highLight; + private Integer isDir; +} From 6f92a8a221e23859be5b7831c2206daebb9cab30 Mon Sep 17 00:00:00 2001 From: MAC <1162714483@qq.com> Date: Mon, 11 Apr 2022 23:27:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(=E6=90=9C=E7=B4=A2=E4=BF=AE=E5=A4=8D):?= =?UTF-8?q?=20=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/qiwenshare/file/config/es/FileSearch.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/qiwenshare/file/config/es/FileSearch.java b/src/main/java/com/qiwenshare/file/config/es/FileSearch.java index e8b4467..0b55320 100644 --- a/src/main/java/com/qiwenshare/file/config/es/FileSearch.java +++ b/src/main/java/com/qiwenshare/file/config/es/FileSearch.java @@ -1,8 +1,10 @@ package com.qiwenshare.file.config.es; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; @Data +@JsonIgnoreProperties(ignoreUnknown = true) public class FileSearch { private String indexName; private Long userFileId;