FileController.java 5.3 KB

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