自动化立体仓库 - WMS系统
dubin
昨天 cccca64a5f5126f04fbe9a0f7eeb80654d2b380b
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
package com.zy.asrs.controller;
 
import com.zy.asrs.utils.ImagePreviewUtils;
import com.zy.common.web.BaseController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
 
@RestController
@RequestMapping()
public class ImagePreviewController extends BaseController {
    //上传图片
    @ApiResponses({@ApiResponse(code = 200, message = "上传成功"), @ApiResponse(code = 400, message = "上传失败"), @ApiResponse(code = 500, message = "服务器内部错误")})
    //@RequestMapping(value = "/saveImage", method = RequestMethod.POST)
    public Map<String, Object> saveImage(@RequestParam("files") MultipartFile[] files) {
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            MultipartFile mfile = files[i];
            //获取文件后缀
            String suffixName = ImagePreviewUtils.getSaveImagePath(mfile);
            //生成新文件名称
            String newFileName = ImagePreviewUtils.getNewImageFileName(suffixName);
            //保存文件
            File file = new File(ImagePreviewUtils.getNewImagePath(newFileName));
            boolean state = ImagePreviewUtils.saveImage(mfile, file);
            if (state) {
//                 list.add(ImageUtil.getNewImagePath(newFileName));
                //保存数据库的图片路径为  相对路径
                list.add("uploadimage/" + newFileName);
            }
        }
        map.put("imgList", list);
        return map;
 
    }
 
    //预览图片
    //@RequestMapping("previewImage")
    @ApiOperation(value = "根据文件名实现预览功能")
    public void previewFile(
            HttpServletResponse response) throws IOException {
        showImg("D:\\"+"images\\"+"20251022efda188c-56b3-4c38-9940-78d0b61b43a1.png", response);
    }
 
    public static void showImg(String path, HttpServletResponse response){
        if(path!=null&&!path.equals("")){
 
            try {
                FileInputStream fis = new FileInputStream(path);
                ServletOutputStream os = response.getOutputStream();
 
                byte [] b = new byte[1024*8];
                while(fis.read(b)!=-1){
                    os.write(b);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}