提交 c86fa3a8 作者: ZhangJingKun

修改保存 zhangjingkun

上级 a0b877ff
......@@ -31,7 +31,6 @@ public class KbFileEditKnowledgeController {
/**
* 临时保存,保存并审核通过, 保存并审核不通过
* @param request
* @param knowledge
* @return
*
......
......@@ -7,6 +7,7 @@ import com.zzsn.knowbase.entity.Document;
import com.zzsn.knowbase.entity.DocumentEditParam;
import com.zzsn.knowbase.service.DocumentService;
import com.zzsn.knowbase.util.DocumentException;
import com.zzsn.knowbase.util.file.FileUtil;
import com.zzsn.knowbase.util.file.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
......@@ -72,7 +73,7 @@ public class DocumentServiceImpl implements DocumentService {
// 校验文件实体
preFileCheck(docFile);
fileName = StringUtils.isNotBlank(fileName) ? fileName : docFile.getName();
String fileKey = this.fileKey(docFile, fileName);
String fileKey = FileUtil.fileKey(docFile, fileName);
Document document = Document.builder()
.fileType(fileType)
.title(fileName)
......
......@@ -144,7 +144,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
//Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filePath, "UTF-8"));
// 告知浏览器文件的大小
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
......@@ -252,7 +252,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
//File savedFile = new File("F:\\DataOfHongQuanzheng\\onlyoffice_data\\app_data\\"+fileName);
File savedFile = new File(filesStorage + fileName);
if (null!=((String) jsonObj.get("userdata"))&&((String) jsonObj.get("userdata")).equals("sample userdata")) {
savedFile = new File(filesStorage + "v1" + fileName);
savedFile = new File(filesStorage + fileName);
}
......@@ -303,12 +303,19 @@ public class LocalFileServiceImpl implements ILocalFileService {
KnowFile knowFile = files.get(0);
String filePath = knowFile.getFilePath();
try {
getDoucmentEditStatus(filePath);
getDoucmentEditStatus(filePath,knowFile.getFileName());
} catch (ParseException e) {
e.printStackTrace();
result = Result.error("文件保存失败!");
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String fileType = fileUtility.getFileType(knowFile.getFileName());
knowFile.setFileType(fileType);
File file = new File(filesStorage + knowFile.getFilePath());
......@@ -317,7 +324,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
knowledgeService.addKnowledge(knowFile,knowledge,userInfo);
return result;
}
public ResponseEntity<Object> getDoucmentEditStatus(String filePath) throws ParseException {
public ResponseEntity<Object> getDoucmentEditStatus(String filePath,String fileName) throws ParseException {
String url = officeUrl+officeCommand;
Map<String,String> map = new HashMap<String,String>();
map.put("c", "forcesave");
......@@ -325,18 +332,40 @@ public class LocalFileServiceImpl implements ILocalFileService {
if (StringUtils.isBlank(docFileMd5)) {
throw new DocumentException(ErrorCodeEnum.DOC_FILE_MD5_ERROR);
}
String pathShortMd5 = Md5Utils.md5(filesStorage + filePath);
String nameShortMd5 = Md5Utils.md5(filePath);
Hashids hashids = new Hashids(DocumentConstants.HASH_KEY);
// String pathShortMd5 = Md5Utils.md5(filesStorage + filePath);
// String nameShortMd5 = Md5Utils.md5(fileName);
// Hashids hashids = new Hashids(DocumentConstants.HASH_KEY);
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
String key = hashids.encodeHex(String.format("%s%s%s", docFileMd5,pathShortMd5, nameShortMd5));
map.put("key", key);
//String key = hashids.encodeHex(String.format("%s%s%s", docFileMd5,pathShortMd5, nameShortMd5));
File docFile = new File(filesStorage + filePath);
fileName = StringUtils.isNotBlank(fileName) ? fileName : docFile.getName();
String fileKey = FileUtil.fileKey(docFile, fileName);
map.put("key", fileKey);
map.put("userdata", "sample userdata");
JSONObject obj = (JSONObject) new JSONParser().parse(FileUtil.editStatus(url, JSON.toJSONString(map)));
return new ResponseEntity<Object>(obj, HttpStatus.OK);
}
public String fileKey(File docFile, String name) {
String docFileMd5 = Md5Utils.getFileMd5(docFile);
if (StringUtils.isBlank(docFileMd5)) {
log.error("$$$ 构建文件信息失败!计算文件 md5 失败!");
throw new DocumentException(ErrorCodeEnum.DOC_FILE_MD5_ERROR);
}
String pathShortMd5 = Md5Utils.md5(docFile.getAbsolutePath());
String nameShortMd5 = Md5Utils.md5(name);
Hashids hashids = new Hashids(DocumentConstants.HASH_KEY);
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
String key = hashids.encodeHex(String.format("%s%s%s", docFileMd5,pathShortMd5, nameShortMd5));
if (StringUtils.isBlank(key)) {
throw new DocumentException(ErrorCodeEnum.DOC_FILE_KEY_ERROR);
}
return key;
}
/**
* 文件下载
......
package com.zzsn.knowbase.util.file;
import com.zzsn.knowbase.constant.DocumentConstants;
import com.zzsn.knowbase.constant.ErrorCodeEnum;
import com.zzsn.knowbase.util.DocumentException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.hashids.Hashids;
import org.json.simple.JSONObject;
import javax.servlet.http.HttpServletRequest;
......@@ -182,4 +187,22 @@ public class FileUtil {
}
}
public static String fileKey(File docFile, String name) {
String docFileMd5 = Md5Utils.getFileMd5(docFile);
if (StringUtils.isBlank(docFileMd5)) {
System.out.println("$$$ 构建文件信息失败!计算文件 md5 失败!");
throw new DocumentException(ErrorCodeEnum.DOC_FILE_MD5_ERROR);
}
String pathShortMd5 = Md5Utils.md5(docFile.getAbsolutePath());
String nameShortMd5 = Md5Utils.md5(name);
Hashids hashids = new Hashids(DocumentConstants.HASH_KEY);
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
String key = hashids.encodeHex(String.format("%s%s%s", docFileMd5,pathShortMd5, nameShortMd5));
if (StringUtils.isBlank(key)) {
throw new DocumentException(ErrorCodeEnum.DOC_FILE_KEY_ERROR);
}
return key;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论