FileController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.zdd.controller;
  2. import com.zdd.config.IpConfig;
  3. import com.zdd.domain.FileItem;
  4. import com.zdd.exceptions.APIException;
  5. import com.zdd.exceptions.APIExceptionType;
  6. import com.zdd.utils.OfficeManager;
  7. import com.zdd.utils.OfficeUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.*;
  16. import java.util.List;
  17. import java.util.Map;
  18. @Controller
  19. @RequestMapping("/api/file/**")
  20. public class FileController {
  21. private static Logger logger = LoggerFactory.getLogger(FileController.class);
  22. @Autowired
  23. private OfficeManager officeManager;
  24. @RequestMapping("/list")
  25. @ResponseBody
  26. public List<FileItem> getFiles() {
  27. return officeManager.getFiles();
  28. }
  29. @RequestMapping("/upload")
  30. @ResponseBody
  31. public void upload(@RequestParam("file") MultipartFile file) throws APIException, IOException {
  32. if (!file.isEmpty()) {
  33. if (!officeManager.checkExtname(file.getOriginalFilename())) {
  34. throw new APIException(APIExceptionType.FileExtError);
  35. }
  36. if (!officeManager.checkFileSize(file.getSize())) {
  37. throw new APIException(APIExceptionType.FileTooLarge);
  38. }
  39. officeManager.save(file.getInputStream(), file.getOriginalFilename());
  40. }
  41. }
  42. @RequestMapping("/download")
  43. public void download(String id, HttpServletResponse response) throws IOException {
  44. FileItem fileItem = officeManager.findById(id);
  45. if (fileItem != null) {
  46. response.setHeader("Content-Disposition", "attachment; filename=" + fileItem.getFileName());
  47. officeManager.read(response.getOutputStream(), fileItem.getFileName());
  48. }
  49. }
  50. @RequestMapping("/edit")
  51. public String onlineEdit(@RequestParam(name = "id", required = true) String id,
  52. @RequestParam(name = "edit", required = false, defaultValue = "false") boolean edit,
  53. String userId,
  54. String userName,
  55. String type, Map<String, Object> map) {
  56. FileItem file = officeManager.findById(id);
  57. String ip = IpConfig.getInetAddress().getHostAddress();
  58. String port = IpConfig.getPort();
  59. String localhost = "http://" + ip + ":" + port;
  60. if (file != null) {
  61. String fileName = file.getFileName();
  62. String fileType = file.getFileType();
  63. String[] split = fileName.split("\\.");
  64. map.put("url", localhost + "/localfile/" + fileName);
  65. map.put("apiurl", officeManager.getOfficeSettings().getApiUrl());
  66. map.put("fileName", fileName);
  67. map.put("fileType", fileType);
  68. map.put("callback", localhost + "/api/online/recallback/" + split[0] + "/" + split[1]);
  69. map.put("docType", OfficeUtils.GetFileType(file.getFileName()));
  70. map.put("key", id);
  71. map.put("userId", userId);
  72. map.put("userName", userName);
  73. map.put("type", type);
  74. map.put("edit", edit);
  75. return "/onlyoffice";
  76. }
  77. return "error";
  78. }
  79. // @RequestMapping("/edit")
  80. // public String onlineEdit(Map<String, Object> map,
  81. // @RequestParam(name = "id", required = true) String id,
  82. // @RequestParam(name = "edit", required = false, defaultValue = "false") boolean edit,
  83. // String userId,
  84. // String userName,
  85. // String type,
  86. // HttpServletRequest request) throws UnsupportedEncodingException {
  87. // FileItem file = officeManager.findById(id);
  88. // if (file != null) {
  89. // map.put("api", officeManager.getOfficeSettings().getApiUrl());
  90. // map.put("fileName", file.getFileName());
  91. // map.put("fileType", file.getFileType());
  92. // map.put("docType", OfficeUtils.GetFileType(file.getFileName()));
  93. // map.put("downloadUrl", ip + "/api/file/download?id=" + file.getId());
  94. // String callbackQuery = "?type=callback&fileName=" +
  95. // URLEncoder.encode(file.getFileName(), "UTF-8") +
  96. // "&userId" + URLEncoder.encode(userId, "UTF-8") +
  97. // "&userName=" + URLEncoder.encode(userName, "UTF-8");
  98. // map.put("configCallback", ip + "/api/online/callback" + callbackQuery);
  99. // map.put("callback", ip + "/api/online/callback");
  100. // map.put("id", file.getId());
  101. // map.put("userId", userId);
  102. // map.put("userName", userName);
  103. // map.put("mode", edit ? "edit" : "view");
  104. // map.put("type", type);
  105. // return "/online";
  106. // }
  107. // return "";
  108. // }
  109. }