|  |  |  | 
|---|
|  |  |  | 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 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) | 
|---|
|  |  |  | @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<>(); | 
|---|
|  |  |  | 
|---|
|  |  |  | //获取文件后缀 | 
|---|
|  |  |  | String suffixName = ImagePreviewUtils.getSaveImagePath(mfile); | 
|---|
|  |  |  | //生成新文件名称 | 
|---|
|  |  |  | String newFileName = ImagePreviewUtils.getNewImageFileName(suffixName); | 
|---|
|  |  |  | //String newFileName = ImagePreviewUtils.getNewImageFileName(suffixName); | 
|---|
|  |  |  | //保存文件 | 
|---|
|  |  |  | File file = new File(ImagePreviewUtils.getNewImagePath(newFileName)); | 
|---|
|  |  |  | File file = new File(ImagePreviewUtils.getNewImagePath(mfile.getOriginalFilename())); | 
|---|
|  |  |  | boolean state = ImagePreviewUtils.saveImage(mfile, file); | 
|---|
|  |  |  | if (state) { | 
|---|
|  |  |  | //                 list.add(ImageUtil.getNewImagePath(newFileName)); | 
|---|
|  |  |  | //保存数据库的图片路径为  相对路径 | 
|---|
|  |  |  | list.add("uploadimage/" + newFileName); | 
|---|
|  |  |  | list.add("uploadimage/" + mfile.getOriginalFilename()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | map.put("imgList", list); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //预览图片 | 
|---|
|  |  |  | //@RequestMapping("previewImage") | 
|---|
|  |  |  | @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("")){ | 
|---|
|  |  |  | 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(path); | 
|---|
|  |  |  | FileInputStream fis = new FileInputStream("D:\\"+"images\\"+maktx+".png"); | 
|---|
|  |  |  | ServletOutputStream os = response.getOutputStream(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | byte [] b = new byte[1024*8]; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|