提交 89af4415 作者: ZhangJingKun

文件上传 zhangjingkun

上级 6b6756c3
......@@ -254,5 +254,14 @@ public class Constants {
//招投标索引
public final static String TENDER = "tender";
public static final Integer MAX_FILE_SIZE = 5 * 1024 * 1024;
public static final Integer CONVERT_TIMEOUT_MS = 120000;
public static final String CONVERTATION_ERROR_MESSAGE_TEMPLATE = "Error occurred in the ConvertService: ";
public static final Long FULL_LOADING_IN_PERCENT = 100L;
public static final Integer FILE_SAVE_TIMEOUT = 5000;
public static final Integer MAX_KEY_LENGTH = 20;
public static final Integer ANONYMOUS_USER_ID = 4;
public static final Integer KILOBYTE_SIZE = 1024;
private Constants() { }
}
......@@ -17,7 +17,8 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class KnowFile{
private String fileId;
private String fileName;
private String filePath;
private String fileType;
private Integer fileSize;
private Long fileSize;
}
/**
*
* (c) Copyright Ascensio System SIA 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.zzsn.knowbase.enums;
public enum DocumentType {
word,
cell,
slide
}
package com.zzsn.knowbase.service;
import com.zzsn.knowbase.entity.Knowledge;
import com.zzsn.knowbase.vo.Result;
import org.springframework.web.multipart.MultipartFile;
/**
* @Version 1.0
* @Author: ZhangJingKun
* @Date: 2024/1/9 9:34
* @Content: 文件上传下载服务
*/
public interface ILocalFileService {
Result<Knowledge> upload(MultipartFile file, String uid);
}
......@@ -12,9 +12,11 @@ import com.zzsn.knowbase.entity.Knowledge;
import com.zzsn.knowbase.kafka.message.KnowledgeMessage;
import com.zzsn.knowbase.kafka.producer.ProduceInfo;
import com.zzsn.knowbase.service.IKnowledgeService;
import com.zzsn.knowbase.service.ILocalFileService;
import com.zzsn.knowbase.util.*;
import com.zzsn.knowbase.vo.KnowledgeParam;
import com.zzsn.knowbase.vo.KnowledgeVO;
import com.zzsn.knowbase.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequest;
......@@ -32,6 +34,9 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;
import springfox.documentation.spring.web.json.Json;
import javax.servlet.http.HttpServletRequest;
......@@ -64,10 +69,17 @@ class KnowledgeServiceImpl implements IKnowledgeService {
@Autowired
private RedisUtil redisUtil;
@Autowired
private ILocalFileService localFileService;
@Override
public void addKnowledge(HttpServletRequest httpServletRequest, Knowledge knowledge) {
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(httpServletRequest, MultipartHttpServletRequest.class);
MultipartFile file = multipartRequest.getFile("file");
Result<Knowledge> result = localFileService.upload(file,knowledge.getId());
if (null == knowledge.getId()) {
knowledge.setId(codeGenerateUtil.geneIdNo(Constants.FINANCE, 8));
}
......
package com.zzsn.knowbase.service.impl;
import com.zzsn.knowbase.entity.KnowFile;
import com.zzsn.knowbase.entity.Knowledge;
import com.zzsn.knowbase.service.ILocalFileService;
import com.zzsn.knowbase.util.file.FileUtility;
import com.zzsn.knowbase.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.UUID;
/**
* @Version 1.0
* @Author: ZhangJingKun
* @Date: 2024/1/9 9:36
* @Content:
*/
@Service
@Slf4j
public class LocalFileServiceImpl implements ILocalFileService {
@Autowired
private FileUtility fileUtility;
@Value("${files.storage}")
String filesStorage;
@Override
public Result<Knowledge> upload(MultipartFile file, String uid) {
try {
String fullFileName = file.getOriginalFilename(); // get file name
String fileExtension = fileUtility.getFileExtension(fullFileName); // get file extension
long fileSize = file.getSize(); // get file size
// check if the file size exceeds the maximum file size or is less than 0
if (fileUtility.getMaxFileSize() < fileSize || fileSize <= 0) {
Result result = Result.error("文件大小不正确!");
return result;
}
// check if file extension is supported by the editor
if (!fileUtility.getFileExts().contains(fileExtension)) {
Result result = Result.error("不支持的文件类型!");
return result;
}
String fileName = file.getOriginalFilename();
String fileSuffix = getFileSuffix(fileName);
String filePath = getFilePath() + UUID.randomUUID();
byte[] bytes = file.getBytes(); // get file in bytes
//Files.write(Paths.get(filePath), bytes);
file.transferTo(new File(filePath));
KnowFile knowFile = new KnowFile();
knowFile.setFileId(fileName);
knowFile.setFileName(fileName);
knowFile.setFilePath(filePath);
knowFile.setFileType(fileSuffix);
knowFile.setFileSize(fileSize);
Result result = Result.OK(knowFile);
return result; // create user metadata and return it
} catch (Exception e) {
e.printStackTrace();
}
// if the operation of file uploading is unsuccessful, an error occurs
Result result = Result.error("上传文件时出现问题!");
return result;
}
private String getFilePath(){
LocalDate currentDate = LocalDate.now();
//System.out.println("当前日期: " + currentDate);
String filePath = filesStorage + currentDate + "\\";
//判断文件夹是否存在,不存在创建
Path directory = Paths.get(filePath);
if (!Files.exists(directory)) {
try {
Files.createDirectories(directory);
log.info("文件夹创建成功:" + filePath);
} catch (IOException e) {
log.error("文件夹创建失败:" + filePath);
e.printStackTrace();
}
}
return filePath;
}
private String getFileSuffix(String fileName){
int lastIndexOfDot = fileName.lastIndexOf('.');
String fileExtension = "";
if (lastIndexOfDot != -1) {
fileExtension = fileName.substring(lastIndexOfDot + 1);
}
return fileExtension;
}
}
/**
*
* (c) Copyright Ascensio System SIA 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.zzsn.knowbase.util.file;
import com.zzsn.knowbase.constant.Constants;
import com.zzsn.knowbase.enums.DocumentType;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
@Qualifier("default")
public class DefaultFileUtility implements FileUtility {
@Value("${filesize-max}")
private String filesizeMax;
@Value("${files.docservice.viewed-docs}")
private String docserviceViewedDocs;
@Value("${files.docservice.edited-docs}")
private String docserviceEditedDocs;
@Value("${files.docservice.convert-docs}")
private String docserviceConvertDocs;
@Value("${files.docservice.fillforms-docs}")
private String docserviceFillDocs;
// document extensions
private List<String> extsDocument = Arrays.asList(
".doc", ".docx", ".docm",
".dot", ".dotx", ".dotm",
".odt", ".fodt", ".ott", ".rtf", ".txt",
".html", ".htm", ".mht", ".xml",
".pdf", ".djvu", ".fb2", ".epub", ".xps", ".oform");
// spreadsheet extensions
private List<String> extsSpreadsheet = Arrays.asList(
".xls", ".xlsx", ".xlsm", ".xlsb",
".xlt", ".xltx", ".xltm",
".ods", ".fods", ".ots", ".csv");
// presentation extensions
private List<String> extsPresentation = Arrays.asList(
".pps", ".ppsx", ".ppsm",
".ppt", ".pptx", ".pptm",
".pot", ".potx", ".potm",
".odp", ".fodp", ".otp");
// get the document type
public DocumentType getDocumentType(final String fileName) {
String ext = getFileExtension(fileName).toLowerCase(); // get file extension from its name
// word type for document extensions
if (extsDocument.contains(ext)) {
return DocumentType.word;
}
// cell type for spreadsheet extensions
if (extsSpreadsheet.contains(ext)) {
return DocumentType.cell;
}
// slide type for presentation extensions
if (extsPresentation.contains(ext)) {
return DocumentType.slide;
}
// default file type is word
return DocumentType.word;
}
// get file name from its URL
public String getFileName(final String url) {
if (url == null) {
return "";
}
// get file name from the last part of URL
String fileName = url.substring(url.lastIndexOf('/') + 1);
fileName = fileName.split("\\?")[0];
return fileName;
}
// get file name without extension
public String getFileNameWithoutExtension(final String url) {
String fileName = getFileName(url);
if (fileName == null) {
return null;
}
String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf('.'));
return fileNameWithoutExt;
}
// get file extension from URL
public String getFileExtension(final String url) {
String fileName = getFileName(url);
if (fileName == null) {
return null;
}
String fileExt = fileName.substring(fileName.lastIndexOf("."));
return fileExt.toLowerCase();
}
// get an editor internal extension
public String getInternalExtension(final DocumentType type) {
// .docx for word file type
if (type.equals(DocumentType.word)) {
return ".docx";
}
// .xlsx for cell file type
if (type.equals(DocumentType.cell)) {
return ".xlsx";
}
// .pptx for slide file type
if (type.equals(DocumentType.slide)) {
return ".pptx";
}
// the default file type is .docx
return ".docx";
}
public List<String> getFillExts() {
return Arrays.asList(docserviceFillDocs.split("\\|"));
}
// get file extensions that can be viewed
public List<String> getViewedExts() {
return Arrays.asList(docserviceViewedDocs.split("\\|"));
}
// get file extensions that can be edited
public List<String> getEditedExts() {
return Arrays.asList(docserviceEditedDocs.split("\\|"));
}
// get file extensions that can be converted
public List<String> getConvertExts() {
return Arrays.asList(docserviceConvertDocs.split("\\|"));
}
// get all the supported file extensions
public List<String> getFileExts() {
List<String> res = new ArrayList<>();
res.addAll(getViewedExts());
res.addAll(getEditedExts());
res.addAll(getConvertExts());
res.addAll(getFillExts());
return res;
}
// generate the file path from file directory and name
public Path generateFilepath(final String directory, final String fullFileName) {
String fileName = getFileNameWithoutExtension(fullFileName); // get file name without extension
String fileExtension = getFileExtension(fullFileName); // get file extension
Path path = Paths.get(directory + fullFileName); // get the path to the files with the specified name
for (int i = 1; Files.exists(path); i++) { // run through all the files with the specified name
// get a name of each file without extension and add an index to it
fileName = getFileNameWithoutExtension(fullFileName) + "(" + i + ")";
// create a new path for this file with the correct name and extension
path = Paths.get(directory + fileName + fileExtension);
}
path = Paths.get(directory + fileName + fileExtension);
return path;
}
// get maximum file size
public long getMaxFileSize() {
long size = Long.parseLong(filesizeMax);
return size > 0 ? size : Constants.MAX_FILE_SIZE;
}
}
/**
*
* (c) Copyright Ascensio System SIA 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.zzsn.knowbase.util.file;
import com.zzsn.knowbase.enums.DocumentType;
import java.nio.file.Path;
import java.util.List;
// specify the file utility functions
public interface FileUtility {
DocumentType getDocumentType(String fileName); // get the document type
String getFileName(String url); // get file name from its URL
String getFileNameWithoutExtension(String url); // get file name without extension
String getFileExtension(String url); // get file extension from URL
String getInternalExtension(DocumentType type); // get an editor internal extension
List<String> getFileExts(); // get all the supported file extensions
List<String> getFillExts(); // get file extensions that can be filled
List<String> getViewedExts(); // get file extensions that can be viewed
List<String> getEditedExts(); // get file extensions that can be edited
List<String> getConvertExts(); // get file extensions that can be converted
Path generateFilepath(String directory, String fullFileName); /* generate the file path
from file directory and name */
long getMaxFileSize(); // get maximum file size
}
......@@ -2,6 +2,10 @@ server:
port: 9088
spring:
servlet:
multipart:
max-request-size: 1024MB
max-file-size: 100MB
datasource:
url: jdbc:mysql://114.116.44.11:3306/knowledge?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&AllowPublicKeyRetrieval=True
username: ciglobal
......@@ -44,3 +48,15 @@ know:
thirdpartyurl:
checkuserurl: http://127.0.0.1:9988/sys/checkToken
getusersurl: http://127.0.0.1:9988/sys/user/thirdparty
files:
storage: E:\\aaa\\
docservice:
fillforms-docs: .docx|.oform
viewed-docs: .djvu|.oxps|.pdf|.xps
edited-docs: .csv|.docm|.docx|.docxf|.dotm|.dotx|.epub|.fb2|.html|.odp|.ods|.odt|.otp|.ots|.ott|.potm|.potx|.ppsm|.ppsx|.pptm|.pptx|.rtf|.txt|.xlsm|.xlsx|.xltm|.xltx
convert-docs: .doc|.dot|.dps|.dpt|.epub|.et|.ett|.fb2|.fodp|.fods|.fodt|.htm|.html|.mht|.mhtml|.odp|.ods|.odt|.otp|.ots|.ott|.pot|.pps|.ppt|.rtf|.stw|.sxc|.sxi|.sxw|.wps|.wpt|.xls|.xlsb|.xlt|.xml
timeout: 120000
history:
postfix: -hist
filesize-max: 5242880
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论