|
@@ -0,0 +1,147 @@
|
|
|
+package com.factory.controller.web;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.factory.base.entity.aggregates.ResponseBean;
|
|
|
+import com.factory.base.util.res.ResponseBeanBuilder;
|
|
|
+import com.factory.web.entity.pm.OnsiteTitleBlob;
|
|
|
+import com.factory.web.service.FileService;
|
|
|
+import com.factory.web.service.impl.DownLoadParam;
|
|
|
+import com.factory.web.service.impl.FileEntry;
|
|
|
+import com.factory.web.service.impl.FileResultInfo;
|
|
|
+import com.factory.web.service.impl.FileServiceImpl;
|
|
|
+import com.factory.web.service.pm.OnsiteTitleBlobService;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+
|
|
|
+
|
|
|
+@Api(tags = "本特勒 - 附件上传下载")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/benteler/fileupload")
|
|
|
+public class FileBlobController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileService fileService;
|
|
|
+ @Autowired
|
|
|
+ private OnsiteTitleBlobService blobService;
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("/addFile")
|
|
|
+ @ApiOperation(value = "上传附件-附件参数名称file")
|
|
|
+ public ResponseBean addFile(@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
+ FileEntry fileEntry = FileEntry.builder().file(file.getBytes())
|
|
|
+ .fileNm(file.getOriginalFilename()).build();
|
|
|
+ fileService.save(fileEntry);
|
|
|
+ FileResultInfo result = FileResultInfo.builder().id(fileEntry.getId()).fileNm(fileEntry.getFileNm()).build();
|
|
|
+ return ResponseBeanBuilder.ok(result);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/downloadFile")
|
|
|
+ @ApiOperation(value = "按id下载附件测试")
|
|
|
+ public void downFile(@RequestParam("id") long id, HttpServletResponse response) throws IOException {
|
|
|
+ download(id, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/downloadFileForPlan")
|
|
|
+ @ApiOperation(value = "按计划id下载附件纠错")
|
|
|
+ public void downloadFileForPlan(DownLoadParam param, HttpServletResponse response) throws IOException {
|
|
|
+
|
|
|
+ long id = -1;
|
|
|
+ if (param.getType() == 2) {
|
|
|
+ OnsiteTitleBlob blob = blobService.queryBlobForPlanId(param.getPlanId());
|
|
|
+ if (blob != null) {
|
|
|
+ blob.setTitleContentStr(new String(blob.getTitleContent(), StandardCharsets.UTF_8));
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(blob.getTitleContentStr());
|
|
|
+ String uploadAttachmentId = jsonObject.getString("uploadAttachmentId");
|
|
|
+ if (uploadAttachmentId != null) {
|
|
|
+ id = Long.parseLong(uploadAttachmentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ download(id, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void download(long id, HttpServletResponse response) {
|
|
|
+ FileEntry file = fileService.getById(id);
|
|
|
+ byte[] buff = null;
|
|
|
+ if (file != null) {
|
|
|
+ String[] suffix = file.getFileNm().split("\\.");
|
|
|
+ String fileType = suffix[suffix.length - 1];
|
|
|
+ if ("jpg,jepg,jpeg,gif,png".contains(fileType)) {//图片类型
|
|
|
+ response.setContentType("image/" + fileType);
|
|
|
+ } else if ("pdf".contains(fileType)) {//pdf类型
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+ } else if ("xls".contains(fileType)) {//pdf类型
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ } else if ("xlsx".contains(fileType)) {//pdf类型
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ } else {//自动判断下载文件类型
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ String fileName = System.currentTimeMillis() + "." + suffix[suffix.length - 1];
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
|
|
+ }
|
|
|
+ buff = file.getFile();
|
|
|
+ } else {
|
|
|
+ response.setContentType("text/plain");
|
|
|
+ buff = new String("no file").getBytes(StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ outputStream.write(buff);
|
|
|
+ outputStream.flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/addIniPlan")
|
|
|
+ public void addIniPlan(HttpServletRequest request) {
|
|
|
+
|
|
|
+ String data = "\n" +
|
|
|
+ "{\n" +
|
|
|
+ " \"uploadAttachmentId\" : 17,\n" +
|
|
|
+ " \"test\":11111111\n" +
|
|
|
+ "}";
|
|
|
+ OnsiteTitleBlob testEntry = OnsiteTitleBlob.builder().bentelerPlanId(Long.parseLong("1"))
|
|
|
+ .titleContent(data.getBytes(StandardCharsets.UTF_8)).build();
|
|
|
+ blobService.save(testEntry);
|
|
|
+ System.out.println("11111111111111111111111111111111111111111");
|
|
|
+ }
|
|
|
+
|
|
|
+// @GetMapping("/getIniPlan")
|
|
|
+// public void getIniPlan(HttpServletRequest request) {
|
|
|
+// Long versionId = Long.valueOf(2);
|
|
|
+// FileEntry pp= testService.getById(versionId);
|
|
|
+// pp.setTitleContentStr(new String(pp.getTitleContent(),StandardCharsets.UTF_8));
|
|
|
+// System.out.println(pp.getTitleContentStr());
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+}
|