puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
package com.sinata.rest.modular.system.controller.common;
 
import com.sinata.rest.common.ApiUtils;
import com.sinata.rest.config.properties.RestProperties;
import com.sinata.rest.core.huawei.obs.ObsUploadUtil;
import com.sinata.rest.core.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
@RestController
@RequestMapping("/imgs")
@Api(tags="API通用接口", description = "字典、鉴权加密、图片地址")
public class ImgsController {
 
    @Autowired
    private RestProperties restProperties;
 
    /**
     * 返回图片
     */
    @RequestMapping(value = "/{pictureId}", method = RequestMethod.GET)
    @ApiOperation(value = "本地图片地址:接口地址+“/imgs/”+图片文件", notes = "图片地址")
    public void renderPicture(@PathVariable("pictureId") String pictureId, HttpServletResponse response) {
        String path = restProperties.getFileUploadPath() + pictureId;
        try {
            byte[] bytes = FileUtil.toByteArray(path);
            response.getOutputStream().write(bytes);
        } catch (Exception e) {
            try {
                //如果找不到图片就返回一个默认图片
                response.sendRedirect("errorPic.png");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
 
    @PostMapping(value = "/upload")
    @ApiOperation(value = "上传图片", notes = "上传图片", response = ApiUtils.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dir", value = "文件目录", dataType = "String", paramType = "query", required = true),
    })
    public Object upload(MultipartFile file, String dir) {
        String pictureName = null;
        try {
            if (file == null) {
                return ApiUtils.returnNG(null, "上传文件为空!");
            }
            pictureName = ObsUploadUtil.obsUpload(file, dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ApiUtils.returnOK(pictureName);
    }
 
}