修改文件上传功能
This commit is contained in:
parent
7265a29a6c
commit
7b29058f1e
@ -32,7 +32,7 @@ public interface DocumentService {
|
|||||||
* @param documentKey
|
* @param documentKey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Document getDocumentToken(String documentKey,String token);
|
Document getDocumentToken(String documentKey,String token, String uuid);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,7 +57,7 @@ public interface DocumentService {
|
|||||||
* @param userName
|
* @param userName
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token);
|
DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token,String uuid);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编辑后保存文档实体文件
|
* 编辑后保存文档实体文件
|
||||||
|
@ -10,6 +10,7 @@ import org.apache.commons.io.FileUtils;
|
|||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.dom4j.DocumentException;
|
||||||
import org.hashids.Hashids;
|
import org.hashids.Hashids;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -132,7 +133,7 @@ public class DocumentServiceImpl implements DocumentService{
|
|||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Document getDocumentToken(String documentKey,String token) {
|
public Document getDocumentToken(String documentKey,String token, String uuid) {
|
||||||
Document doc = null;
|
Document doc = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -145,7 +146,7 @@ public class DocumentServiceImpl implements DocumentService{
|
|||||||
}
|
}
|
||||||
// 从缓存中取出后,再绑定非必需缓存字段(节省缓存大小)
|
// 从缓存中取出后,再绑定非必需缓存字段(节省缓存大小)
|
||||||
// doc.setKey(documentKey);
|
// doc.setKey(documentKey);
|
||||||
doc.setUrl(fileUrlToken(doc.getTitle(),token));
|
doc.setUrl(fileUrlToken(doc.getTitle(),token,uuid));
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.info(doc.toString());
|
log.info(doc.toString());
|
||||||
}
|
}
|
||||||
@ -199,9 +200,8 @@ public class DocumentServiceImpl implements DocumentService{
|
|||||||
* @param
|
* @param
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private String fileUrlToken(String filename,String token) {
|
private String fileUrlToken(String filename,String token, String uuid) {
|
||||||
return String.format(DocumentConstants.OFFICE_API_DOC_FILE, getServerHost(), "?name="+filename+"&token="+token);
|
return String.format(DocumentConstants.OFFICE_API_DOC_FILE, getServerHost(), "?name="+filename+"&uuid="+uuid+"&token="+token);
|
||||||
// return "http://192.168.0.58:20053/download?name="+filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -241,10 +241,9 @@ public class DocumentServiceImpl implements DocumentService{
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token) {
|
public DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token, String uuid) {
|
||||||
return DocumentEditParam.builder()
|
return DocumentEditParam.builder()
|
||||||
.callbackUrl(callbackUrl(fileName)+"&token="+token)
|
.callbackUrl(callbackUrl(fileName)+"&uuid="+uuid+"&token="+token)
|
||||||
// .callbackUrl(callbackUrl(fileName))
|
|
||||||
.user(DocumentEditParam.UserBean.builder()
|
.user(DocumentEditParam.UserBean.builder()
|
||||||
.id(userId)
|
.id(userId)
|
||||||
.name(userName)
|
.name(userName)
|
||||||
|
@ -96,7 +96,6 @@ public class onlyofController {
|
|||||||
@Operation(summary = "获取文件列表")
|
@Operation(summary = "获取文件列表")
|
||||||
@PreAuthorize("@ss.hasPermission('only:onlyof:filelist')")
|
@PreAuthorize("@ss.hasPermission('only:onlyof:filelist')")
|
||||||
public ResponseEntity<Object> listFile() {
|
public ResponseEntity<Object> listFile() {
|
||||||
System.out.println("fileList");
|
|
||||||
return new ResponseEntity<Object>(uploadService.list(), HttpStatus.OK);
|
return new ResponseEntity<Object>(uploadService.list(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,47 +106,30 @@ public class onlyofController {
|
|||||||
* @return
|
* @return
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@PostMapping("/update")
|
@PostMapping("/update")
|
||||||
@Operation(summary = "更新文件编辑")
|
@Operation(summary = "更新文件编辑")
|
||||||
@PreAuthorize("@ss.hasPermission('only:onlyof:update')")
|
@PreAuthorize("@ss.hasPermission('only:onlyof:update')")
|
||||||
public ResponseEntity<Object> upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception {
|
public ResponseEntity<Object> upload(@RequestParam("file") MultipartFile file,@RequestParam("uuid") String uuid,HttpServletRequest request) throws Exception {
|
||||||
System.out.println("update");
|
uuid = uuid.isEmpty() ? uuid : uuid + "\\";
|
||||||
|
// System.out.println("update"+file.getOriginalFilename()+uuid);
|
||||||
if (file.isEmpty()){
|
if (file.isEmpty()){
|
||||||
throw new Exception("上传文件不能为空");
|
throw new Exception("上传文件不能为空");
|
||||||
}
|
}
|
||||||
onlyofficeDO upload = new onlyofficeDO();
|
onlyofficeDO upload = new onlyofficeDO();
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
//更新保存文件信息到数据库
|
//更新保存文件信息到数据库
|
||||||
FileUtil.saveFile(file.getInputStream(),filePath+file.getOriginalFilename());
|
FileUtil.saveFile(file.getInputStream(),filePath+uuid+file.getOriginalFilename(),filePath+uuid);
|
||||||
upload.setUploadDate(new Date());
|
upload.setUploadDate(new Date());
|
||||||
upload.setFilePath(filePath);
|
upload.setFilePath(filePath+uuid);
|
||||||
upload.setFileName(file.getOriginalFilename());
|
upload.setFileName(file.getOriginalFilename());
|
||||||
upload.setFileSize(file.getSize());
|
upload.setFileSize(file.getSize());
|
||||||
System.out.println("111"+upload);
|
// System.out.println("111"+upload);
|
||||||
uploadService.save(upload);
|
uploadService.save(upload);
|
||||||
System.gc();
|
System.gc();
|
||||||
|
|
||||||
return new ResponseEntity<Object>("上传成功", HttpStatus.OK);
|
return new ResponseEntity<Object>("上传成功", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 下载文档接口
|
|
||||||
* @param name
|
|
||||||
* @param response
|
|
||||||
*/
|
|
||||||
@GetMapping("/download")
|
|
||||||
@Operation(summary = "下载文件")
|
|
||||||
@PreAuthorize("@ss.hasPermission('only:onlyof:download')")
|
|
||||||
public void download(String name, HttpServletResponse response) {
|
|
||||||
// System.out.println("download---"+name+"-----"+response+"-----");
|
|
||||||
try {
|
|
||||||
FileUtil.downLoadFile(name,filePath,response);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编辑文件的接口
|
* 编辑文件的接口
|
||||||
* @param name
|
* @param name
|
||||||
@ -160,24 +142,44 @@ public class onlyofController {
|
|||||||
@GetMapping("/edit")
|
@GetMapping("/edit")
|
||||||
@Operation(summary = "编辑文件")
|
@Operation(summary = "编辑文件")
|
||||||
@PreAuthorize("@ss.hasPermission('only:onlyof:edit')")
|
@PreAuthorize("@ss.hasPermission('only:onlyof:edit')")
|
||||||
public ModelAndView editDocFile(@RequestParam String name, String userName, String userId ,String token, Model model) {
|
public ModelAndView editDocFile(@RequestParam String name, String userName, String userId ,String uuid ,String token, Model model) {
|
||||||
System.out.println("edit");
|
String uuid2 = "";
|
||||||
// System.out.println("token"+token);
|
uuid2 = uuid.isEmpty() ? uuid : uuid + "\\";
|
||||||
String path = filePath+name;
|
String path = filePath+uuid2+name;
|
||||||
// System.out.println("path:"+path);
|
// System.out.println("path:"+path);
|
||||||
|
// System.out.println("edit"+uuid2);
|
||||||
|
// System.out.println("token"+token);
|
||||||
ModelAndView mav = new ModelAndView();
|
ModelAndView mav = new ModelAndView();
|
||||||
Document document = documentService.getDocumentToken(documentService.buildDocument(path, name),token);
|
Document document = documentService.getDocumentToken(documentService.buildDocument(path, name),token,uuid);
|
||||||
mav.addObject("document", document);
|
mav.addObject("document", document);
|
||||||
if (!documentService.canEdit(document)) {
|
if (!documentService.canEdit(document)) {
|
||||||
// System.out.println("demo");
|
// System.out.println("demo");
|
||||||
return mav;
|
return mav;
|
||||||
}
|
}
|
||||||
mav.addObject("documentEditParam", documentService.buildDocumentEditParamToken(userId, userName, name,token));
|
mav.addObject("documentEditParam", documentService.buildDocumentEditParamToken(userId, userName, name,token,uuid2));
|
||||||
mav.setViewName("editor");
|
mav.setViewName("editor");
|
||||||
// System.out.println(mav);
|
// System.out.println(mav);
|
||||||
return mav;
|
return mav;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文档接口
|
||||||
|
* @param name
|
||||||
|
* @param response
|
||||||
|
*/
|
||||||
|
@GetMapping("/download")
|
||||||
|
@Operation(summary = "下载文件")
|
||||||
|
@PreAuthorize("@ss.hasPermission('only:onlyof:download')")
|
||||||
|
public void download(String name, String uuid,HttpServletResponse response) {
|
||||||
|
uuid = uuid.equals("") ? uuid : uuid + "\\";
|
||||||
|
// System.out.println("download: "+name+"---"+response+"---"+uuid);
|
||||||
|
try {
|
||||||
|
FileUtil.downLoadFile(name,filePath+uuid,response);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件编辑打开保存后的回调函数
|
* 文件编辑打开保存后的回调函数
|
||||||
* @param request
|
* @param request
|
||||||
@ -187,10 +189,10 @@ public class onlyofController {
|
|||||||
@RequestMapping("/callback")
|
@RequestMapping("/callback")
|
||||||
@Operation(summary = "回调文件")
|
@Operation(summary = "回调文件")
|
||||||
@PreAuthorize("@ss.hasPermission('only:onlyof:callback')")
|
@PreAuthorize("@ss.hasPermission('only:onlyof:callback')")
|
||||||
public void saveDocumentFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
public void saveDocumentFile(HttpServletRequest request, HttpServletResponse response,String uuid) throws IOException {
|
||||||
System.out.println("callback");
|
// System.out.println("callback"+uuid);
|
||||||
//处理编辑回调逻辑
|
//处理编辑回调逻辑
|
||||||
callBack(request, response);
|
callBack(request, response,uuid);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +206,7 @@ public class onlyofController {
|
|||||||
@Operation(summary = "文件状态")
|
@Operation(summary = "文件状态")
|
||||||
@PreAuthorize("@ss.hasPermission('only:onlyof:editStatus')")
|
@PreAuthorize("@ss.hasPermission('only:onlyof:editStatus')")
|
||||||
public ResponseEntity<Object> getDoucmentEditStatus(String name) throws ParseException {
|
public ResponseEntity<Object> getDoucmentEditStatus(String name) throws ParseException {
|
||||||
System.out.println("editStatus");
|
// System.out.println("editStatus");
|
||||||
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");
|
||||||
@ -230,52 +232,51 @@ public class onlyofController {
|
|||||||
* @param response
|
* @param response
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private void callBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
/* 状态
|
||||||
|
0 - no document with the key identifier could be found,
|
||||||
|
1 - document is being edited,
|
||||||
|
2 - document is ready for saving,
|
||||||
|
3 - document saving error has occurred,
|
||||||
|
4 - document is closed with no changes,
|
||||||
|
6 - document is being edited, but the current document state is saved,
|
||||||
|
7 - error has occurred while force saving the document.
|
||||||
|
* */
|
||||||
|
private void callBack(HttpServletRequest request, HttpServletResponse response, String uuid) throws IOException {
|
||||||
PrintWriter writer = null;
|
PrintWriter writer = null;
|
||||||
JSONObject jsonObj = null;
|
JSONObject jsonObj = null;
|
||||||
System.out.println("===saveeditedfile------------");
|
// System.out.println("===saveeditedfile------------");
|
||||||
try {
|
try {
|
||||||
writer = response.getWriter();
|
writer = response.getWriter();
|
||||||
Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
|
Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
|
||||||
String body = scanner.hasNext() ? scanner.next() : "";
|
String body = scanner.hasNext() ? scanner.next() : "";
|
||||||
jsonObj = (JSONObject) new JSONParser().parse(body);
|
jsonObj = (JSONObject) new JSONParser().parse(body);
|
||||||
// jsonObj = (JSONObject) new JSONParser(body);
|
// System.out.println(jsonObj);
|
||||||
System.out.println(jsonObj);
|
// System.out.println("===saveeditedfile:" + jsonObj.get("status"));
|
||||||
System.out.println("===saveeditedfile:" + jsonObj.get("status"));
|
|
||||||
/*
|
|
||||||
0 - no document with the key identifier could be found,
|
|
||||||
1 - document is being edited,
|
|
||||||
2 - document is ready for saving,
|
|
||||||
3 - document saving error has occurred,
|
|
||||||
4 - document is closed with no changes,
|
|
||||||
6 - document is being edited, but the current document state is saved,
|
|
||||||
7 - error has occurred while force saving the document.
|
|
||||||
* */
|
|
||||||
if ((long) jsonObj.get("status") == 2) {
|
if ((long) jsonObj.get("status") == 2) {
|
||||||
FileUtil.callBackSaveDocument(jsonObj,filePath,request, response);
|
FileUtil.callBackSaveDocument(jsonObj,filePath+uuid,request, response);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (org.json.simple.parser.ParseException e) {
|
} catch (ParseException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* status = 1,我们给onlyoffice的服务返回{"error":"0"}的信息,这样onlyoffice会认为回调接口是没问题的,这样就可以在线编辑文档了,否则的话会弹出窗口说明
|
* status = 1,我们给onlyoffice的服务返回{"error":"0"}的信息,这样onlyoffice会认为回调接口是没问题的,这样就可以在线编辑文档了,否则的话会弹出窗口说明
|
||||||
* 在线编辑还没有关闭,前端有人下载文档时,强制保存最新内容 当status 是6时说明有人在编辑时下载文档
|
* 在线编辑还没有关闭,前端有人下载文档时,强制保存最新内容 当status 是6时说明有人在编辑时下载文档
|
||||||
* */
|
* */
|
||||||
System.out.println(jsonObj.get("status"));
|
// System.out.println(jsonObj.get("status"));
|
||||||
if ((long) jsonObj.get("status") == 6) {
|
if ((long) jsonObj.get("status") == 6) {
|
||||||
//处理当文档正在编辑为关闭时,下载文档
|
//处理当文档正在编辑为关闭时,下载文档
|
||||||
if (((String)jsonObj.get("userdata")).equals("sample userdata")){
|
if (((String)jsonObj.get("userdata")).equals("sample userdata")){
|
||||||
FileUtil.callBackSaveDocument(jsonObj,filePath,request, response);
|
FileUtil.callBackSaveDocument(jsonObj,filePath+uuid,request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("====保存失败:");
|
// System.out.println("====保存失败:");
|
||||||
writer.write("{\"error\":1}");
|
writer.write("{\"error\":1}");
|
||||||
} else {
|
} else {
|
||||||
//执行删除编辑时下载保存的文件:
|
//执行删除编辑时下载保存的文件:
|
||||||
FileUtil.deleteTempFile(filePath,request.getParameter("fileName"));
|
FileUtil.deleteTempFile(filePath+uuid,request.getParameter("fileName"));
|
||||||
writer.write("{\"error\":0}");
|
writer.write("{\"error\":0}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user