自动化立体仓库 - WMS系统
#1
dubin
3 天以前 864c18c46c2db1ae92220181e40b291b3b0d9446
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
package com.zy.asrs.controller;
 
import com.core.common.R;
import com.sun.prism.Image;
import com.zy.asrs.entity.ImageView;
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 lombok.extern.slf4j.Slf4j;
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.nio.file.Files;
import java.util.*;
 
@Slf4j
@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(mfile.getOriginalFilename()));
            boolean state = ImagePreviewUtils.saveImage(mfile, file);
            if (state) {
//                 list.add(ImageUtil.getNewImagePath(newFileName));
                //保存数据库的图片路径为  相对路径
                list.add("uploadimage/" + mfile.getOriginalFilename());
            }
        }
        map.put("imgList", list);
        return map;
 
    }
 
    //预览图片
    @RequestMapping("/previewImage")
    @ApiOperation(value = "根据文件名实现预览功能")
    public String previewFile(
            HttpServletResponse response,String maktx) throws IOException {
//        String[] s = matnr.split("__");
        showImg("D:\\"+"images\\"+maktx+".png", response,maktx);
        if("D:\\"+"images\\"+maktx+".png"!=null){
 
            try {
                FileInputStream fis = new FileInputStream("D:\\"+"images\\"+maktx+".png");
                ServletOutputStream os = response.getOutputStream();
 
                byte [] b = new byte[1024*8];
                while(fis.read(b)!=-1){
                    os.write(b);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "该简图不存在,请上传";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
 
    public static void showImg(String path, HttpServletResponse response,String maktx){
 
    }
    public static R showImgToBase64(String path, HttpServletResponse response,String maktx){
            byte[] imageBytes = null;
            try {
                File file =new File(path);
                imageBytes = Files.readAllBytes(file.toPath());
            }catch (IOException e){
                e.printStackTrace();
                return null;
            }
            String pics = Base64.getEncoder().encodeToString(imageBytes);
            List<String> list = new ArrayList<>();
            List<ImageView> imageViewList = new ArrayList<>();
            ImageView imageView = new ImageView();
            //String s = "http://192.168.0.2/image/"+pics+".jpg";
            list.add(pics);
            imageView.setPics(list);
            imageView.setMaktx(maktx);
            imageViewList.add(imageView);
            //log.info(s);
            log.info(pics);
            return R.ok().add(imageViewList);
    }
}