管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-10-08 e7b3a5e891287b1291d2ac38f7c83d5d73bc7906
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.lf.server.controller.all;
 
import com.lf.server.aspect.SysLog;
import com.lf.server.entity.all.ResponseMsg;
import com.lf.server.helper.FileHelper;
import com.lf.server.service.all.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.Iterator;
 
/**
 * 文件控制器
 * @author WWW
 */
@Api(tags = "运维管理\\文件管理")
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {
    @Autowired
    private FileService fileService;
 
    private final static Log log = LogFactory.getLog(FileController.class);
 
    @SysLog()
    @ApiOperation(value = "文件上传")
    @PostMapping(value = "/upload")
    public ResponseMsg<String> upload(@RequestBody MultipartFile[] files, String tab, HttpServletRequest req, HttpServletResponse res) {
        try {
            MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) req;
            Iterator<String> fileNames = mreq.getFileNames();
 
            // 获取原文件名
            // noinspection AlibabaLowerCamelCaseVariableNaming
            String OriginalFileName = files[0].getOriginalFilename();
            // 获取文件后缀名
            String suffixName = OriginalFileName.substring(OriginalFileName.lastIndexOf("."));
 
            String fileName = null;
            MultipartFile file = null;
            while (fileNames.hasNext()) {
                fileName = (String) fileNames.next();
                file = mreq.getFile(fileName);
            }
 
            // kb
            long size = file.getSize();
            file.transferTo(new File(""));
 
            return success("");
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guid", value = "文件GUID", dataType = "String", paramType = "query", example = "")
    })
    @GetMapping("/download")
    public void download(String guid, HttpServletRequest req, HttpServletResponse res) {
        try {
            String filePath = "";
 
            // 创建文件对象
            File file = new File(filePath);
            if (!file.exists()) {
                return;
            }
 
            String fileName = URLEncoder.encode("学生名册.pdf", "UTF-8");
 
            // 设置响应头中文件的下载方式为附件方式,以及设置文件名
            res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
 
            // 定义输入流,通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(file);
 
            // 通过response对象,获取到输出流
            ServletOutputStream outputStream = res.getOutputStream();
 
            // 设置响应头的编码格式为UTF-8
            res.setCharacterEncoding("UTF-8");
 
            // 通过response对象设置响应数据格式("text/plain; charset=utf-8")
            String ext = FileHelper.getExtension(file);
            String mime = FileHelper.getMime(ext);
            res.setContentType(mime);
 
            // 记录下载记录...
 
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                // 通过输入流读取文件数据,然后通过上述的输出流写回浏览器
                outputStream.write(bytes, 0, len);
                // 刷新
                outputStream.flush();
            }
 
            // 关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception ex) {
            log.error(ex.getStackTrace());
        }
    }
}