直接看代碼吧,主要邏輯吧excel的圖片拿到 壓縮上傳獲取url
// 將文件轉成XSSFWorkbook工作簿XSSFWorkbook wb = new XSSFWorkbook(uploadFile);// 獲取工作薄中第一個excel表格XSSFSheet sheet = wb.getSheetAt(0);// 核心:::獲取excel表格中所有圖片,處理圖片上傳到oss key:行號-列號Map<String, List<String>> picturesMap = getPictures(sheet);public Map<String, List<String>> getPictures(XSSFSheet xssfSheet) throws IOException {Map<String, List<String>> maps = new LinkedHashMap<>();List<XSSFShape> list = xssfSheet.getDrawingPatriarch().getShapes();for (int i = 0; i < list.size(); i++) {XSSFPicture picture = (XSSFPicture) list.get(i);// 行號-列號XSSFClientAnchor xssfClientAnchor = (XSSFClientAnchor) picture.getAnchor();// 獲取圖片XSSFPictureData pdata = picture.getPictureData();byte[] data = pdata.getData();InputStream inputStream = new ByteArrayInputStream(data);byte[] scalePicLater = scalePics(inputStream,0.5,0.5);String url = ossFactory.build().upload(new ByteArrayInputStream(scalePicLater), IdUtil.objectId() + ".jpg");inputStream.close();// 行號-列號String key = xssfClientAnchor.getRow1() - 1 + "-" + xssfClientAnchor.getCol1();if (maps.containsKey(key)) {List<String> strUrl = maps.get(key);strUrl.add(url);maps.put(key, strUrl);} else {List<String> strUrl = new ArrayList<>();strUrl.add(url);maps.put(key, strUrl);}}return maps;}public static byte[] scalePics(InputStream inputStream, double accuracy,double scale) throws IOException {// 壓縮圖片并保存到臨時文件中File tempFile = File.createTempFile("thumbnail", ".jpg");Thumbnails.of(inputStream).scale(scale).outputQuality(accuracy).toFile(tempFile);// 讀取臨時文件的字節流設置到輸出流中InputStream tempInputStream = new FileInputStream(tempFile);byte[] buffer = new byte[tempInputStream.available()];tempInputStream.read(buffer);tempInputStream.close();// 刪除臨時文件tempFile.delete();// 下載到本地,// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Code\\upload\\1.jpg"));// bos.write(buffer);// bos.close();return buffer;}