/src/main/java/com/eastrobot/doc/web/WatermarkController.java

https://github.com/ekoz/kbase-doc · Java · 81 lines · 55 code · 6 blank · 20 comment · 1 complexity · 1c80ee161a2f8620135f544e5b862241 MD5 · raw file

  1. /*
  2. * Power by www.xiaoi.com
  3. */
  4. package com.eastrobot.doc.web;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.Date;
  8. import com.eastrobot.doc.config.BaseController;
  9. import com.eastrobot.doc.service.WatermarkService;
  10. import org.apache.commons.io.FileUtils;
  11. import org.apache.commons.io.FilenameUtils;
  12. import org.apache.commons.lang3.time.DateFormatUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.core.io.ByteArrayResource;
  15. import org.springframework.http.HttpHeaders;
  16. import org.springframework.http.ResponseEntity;
  17. import org.springframework.util.ResourceUtils;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import io.swagger.annotations.ApiImplicitParam;
  24. import io.swagger.annotations.ApiImplicitParams;
  25. import io.swagger.annotations.ApiOperation;
  26. import lombok.extern.slf4j.Slf4j;
  27. import springfox.documentation.service.ResponseMessage;
  28. /**
  29. * @author <a href="mailto:eko.z@outlook.com">eko.zhan</a>
  30. * @date 2018年9月2日 下午2:00:44
  31. * @version 1.0
  32. */
  33. @Slf4j
  34. @RestController
  35. @RequestMapping("/watermark")
  36. public class WatermarkController extends BaseController {
  37. @Autowired
  38. WatermarkService watermarkService;
  39. /**
  40. * 输出文件demo https://o7planning.org/en/11765/spring-boot-file-download-example
  41. * @author eko.zhan at 2018年9月2日 下午4:26:10
  42. * @param file
  43. * @param text
  44. * @param color
  45. * @return
  46. * @throws IOException
  47. */
  48. @ApiOperation(value="传入文件并返回水印文件", response=ResponseMessage.class)
  49. @ApiImplicitParams({
  50. @ApiImplicitParam(name="file", value="待添加水印的文件", dataType="__file", required=true, paramType="form"),
  51. @ApiImplicitParam(name="text", value="水印内容", dataType="string", required=false, paramType="form"),
  52. @ApiImplicitParam(name="color", value="颜色码,以#开头", dataType="string", required=false, paramType="form")
  53. })
  54. @PostMapping("/handle")
  55. public ResponseEntity<ByteArrayResource> handle(@RequestParam("file") MultipartFile file, @RequestParam(value="text", required=false) String text, @RequestParam(value="color", required=false) String color) throws IOException {
  56. if (!file.getOriginalFilename().toLowerCase().endsWith(".docx")) {
  57. log.error("上传的文件必须是 docx 类型");
  58. }
  59. File dir = ResourceUtils.getFile("classpath:static/DATAS");
  60. String pardir = DateFormatUtils.format(new Date(), "yyyyMMdd");
  61. String filename = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + "." + FilenameUtils.getExtension(file.getOriginalFilename());
  62. File originFile = new File(dir + "/" + pardir + "/" + filename);
  63. FileUtils.copyInputStreamToFile(file.getInputStream(), originFile);
  64. byte[] bytes = watermarkService.handle(originFile, text, color);
  65. ByteArrayResource resource = new ByteArrayResource(bytes);
  66. return ResponseEntity.ok()
  67. // Content-Disposition
  68. .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + originFile.getName())
  69. // Content-Type
  70. .contentType(getMediaType(originFile.getName()))
  71. // Contet-Length
  72. .contentLength(bytes.length)
  73. .body(resource);
  74. }
  75. }