| | |
| | | public class ExcelPictureUtil { |
| | | |
| | | public static Map<String, XSSFPictureData> getExcelPictures(InputStream is) { |
| | | byte[] fileData = getFileStream(is); |
| | | Map<String, XSSFPictureData> pictures = getPictures(fileData); |
| | | pictures.forEach((id, xssfPictureData) -> { |
| | | System.out.println("id:" + id); |
| | | String fileName = xssfPictureData.getPackagePart().getPartName().getName(); |
| | | System.out.println("fileName:" + fileName); |
| | | }); |
| | | return pictures; |
| | | try { |
| | | byte[] fileData = toByteArray(is); |
| | | Map<String, XSSFPictureData> pictures = getPictures(fileData); |
| | | pictures.forEach((id, xssfPictureData) -> { |
| | | System.out.println("id:" + id); |
| | | String fileName = xssfPictureData.getPackagePart().getPartName().getName(); |
| | | System.out.println("fileName:" + fileName); |
| | | }); |
| | | return pictures; |
| | | }catch (Exception e){ |
| | | return null; |
| | | } |
| | | |
| | | } |
| | | public static byte[] toByteArray(InputStream input) throws IOException { |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | int bytesRead; |
| | | byte[] buffer = new byte[1024]; // 可以根据需要调整缓冲区大小 |
| | | while ((bytesRead = input.read(buffer)) != -1) { |
| | | byteArrayOutputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | return byteArrayOutputStream.toByteArray(); |
| | | } |
| | | |
| | | /** |
| | |
| | | // 创建 ByteArrayOutputStream 来暂存流数据 |
| | | try { |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | // 将 inputStream 读取到 byteArrayOutputStream 中 |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | while ((length = inputStream.read(buffer)) != -1) { |