xuefei
2020-12-10 eeeb7233935ea9b10e99043bdbf740ef86c9bf20
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
package cn.cetc54.platform.base.controller.common;
 
import cn.cetc54.platform.base.entity.File;
import cn.cetc54.platform.base.manager.impl.LocalFileManage;
import cn.cetc54.platform.base.service.FileService;
import cn.cetc54.platform.core.common.exception.PlatformException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
 
 
/**
 * @author x
 */
@Slf4j
@Controller
@Api(description = "文件管理管理接口")
@RequestMapping("/platform/file")
@Transactional
public class FileController {
 
    @Autowired
    private FileService fileService;
 
 
 
 
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "本地存储预览文件")
    public void view(@PathVariable String id, HttpServletResponse response) throws IOException {
 
        File file = fileService.get(id);
        if(file==null){
            throw new PlatformException("文件ID:"+id+"不存在");
        }
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getFKey(), "UTF-8"));
        response.setContentLengthLong(file.getSize());
        response.setContentType(file.getType());
        response.addHeader("Accept-Ranges", "bytes");
        if(file.getSize()!=null&&file.getSize()>0){
            response.addHeader("Content-Range", "bytes " + 0 + "-" + (file.getSize()-1) + "/" + file.getSize());
        }
        LocalFileManage.view(file.getUrl(), response);
    }
}