提交 c86fa3a8 作者: ZhangJingKun

修改保存 zhangjingkun

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