浏览代码

代码更新

dingsong 4 年之前
父节点
当前提交
22bb1b6245

+ 30 - 0
src/main/java/com/migao/controller/DocumentSharingController.java

@@ -0,0 +1,30 @@
+package com.migao.controller;
+
+import com.migao.config.response.ResponseBean;
+import com.migao.service.DocumentSharingService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @author dingsong
+ */
+@Api(tags = "006.文档共享模快")
+@RestController
+@RequestMapping("/documentSharing")
+public class DocumentSharingController {
+    @Resource
+    DocumentSharingService documentSharingService;
+
+//    @ApiOperation("下载单个文档")
+//    @PostMapping(value = "/download")
+//    public ResponseBean<?> download(HttpServletResponse response, @RequestParam String id){
+//
+//    }
+}

+ 10 - 0
src/main/java/com/migao/service/DocumentSharingService.java

@@ -11,6 +11,8 @@ import com.migao.entity.vo.req.DocumentSharingUpdateReq;
 import com.migao.entity.vo.res.DocumentSharingPageQueryRes;
 import com.migao.entity.vo.res.DocumentSharingQueryRes;
 
+import javax.servlet.http.HttpServletResponse;
+
 /**
  * @author dingsong
  */
@@ -72,4 +74,12 @@ public interface DocumentSharingService extends IService<DocumentSharing> {
      * @return
      */
     ResponseBean<PageBeanOne<DocumentSharingPageQueryRes>> pageQueryOneById(DocumentSharingQueryReq documentSharingQuery);
+
+    /**
+     * 下载单个文件
+     * @param response 响应
+     * @param id 文档共享id
+     * @return
+     */
+    ResponseBean<?> download(HttpServletResponse response, String id);
 }

+ 41 - 0
src/main/java/com/migao/service/impl/DocumentSharingServiceImpl.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.migao.config.authority.LoginUtils;
+import com.migao.config.properties.SystemProperties;
 import com.migao.config.response.*;
 import com.migao.entity.po.*;
 import com.migao.entity.vo.req.DocumentSharingInsertReq;
@@ -23,6 +24,11 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.time.LocalDateTime;
 
 /**
@@ -44,6 +50,8 @@ public class DocumentSharingServiceImpl extends ServiceImpl<DocumentSharingMappe
     FileInfoMapper fileInfoMapper;
     @Resource
     UserMapper userMapper;
+    @Resource
+    private SystemProperties systemProperties;
 
     @Override
     public ResponseBean<?> insert(DocumentSharingInsertReq doc) {
@@ -295,5 +303,38 @@ public class DocumentSharingServiceImpl extends ServiceImpl<DocumentSharingMappe
         );
     }
 
+    @Override
+    public ResponseBean<?> download(HttpServletResponse response, String id) {
+        Integer idInt = Integer.parseInt(id);
+        DocumentSharing documentSharing = documentSharingMa.selectById(idInt);
+        FileInfo fileInfo = fileInfoMapper.selectById(documentSharing);
+        String filePath = StringUtils.join(systemProperties.getDownloadPrefix(), fileInfo.getUri());
+        File file = new File(filePath);
+
+        // 取得文件名。
+        String fileName = file.getName();
+        InputStream fis = null;
+        try {
+            fis = new FileInputStream(file);
+            response.reset();
+            response.setCharacterEncoding("UTF-8");
+            response.setContentType("application/force-download");
+            response.addHeader("Content-Disposition",
+                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));
+            response.setHeader("Content-Length", String.valueOf(file.length()));
+
+            byte[] b = new byte[1024];
+            int len;
+            while ((len = fis.read(b)) != -1) {
+                response.getOutputStream().write(b, 0, len);
+            }
+            response.flushBuffer();
+            fis.close();
+            return ResponseBuilder.ok();
+        }catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
 
 }