直接保存到本地磁盘
public static void main(String[] args) throws IOException {
String excelFilePath = "F:/ftproot/METAMSC/20231011/fskbook.xlsx";
String imagePath = "D:/temp";
try {
FileInputStream fis = new FileInputStream(new File(excelFilePath));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFDrawing drawings = sheet.getDrawingPatriarch();
List<XSSFShape> shapes = drawings.getShapes();
int i=0;
for (XSSFShape shape : shapes) {
if (shape instanceof XSSFPicture) {
++i;
if (i ==4){
continue;
}
XSSFPicture picture = (XSSFPicture) shape;
PictureData pictureData = picture.getPictureData();
byte[] data = pictureData.getData();
String imageFileName = UUID.randomUUID().toString() + ".jpg"; // Generate unique file name
FileOutputStream fos =
new FileOutputStream(new File(imagePath + File.separator + imageFileName));
fos.write(data);
fos.flush();
fos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
上传到ftp上面
/**
*
* @param ftpServerIp
* @param port
* @param user
* @param pass
* @param ftpUploadPath
* @param fis
* @return
*/
public static List<String> extractExcelImages(String ftpServerIp, int port, String user, String pass, String ftpUploadPath, FileInputStream fis) {
List<String> ftpPath = new ArrayList<>();
ftpUploadPath += DateUtil.format(new Date(), "yyyyMMddHHmmss");
try {
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFDrawing drawings = sheet.getDrawingPatriarch();
List<XSSFShape> shapes = drawings.getShapes();
for (XSSFShape shape : shapes) {
if (shape instanceof XSSFPicture) {
XSSFPicture picture = (XSSFPicture) shape;
PictureData pictureData = picture.getPictureData();
byte[] data = pictureData.getData();
String imageFileName = UUID.randomUUID().toString() + ".jpg"; // Generate unique file name
InputStream inputStream = new ByteArrayInputStream(data);
Ftp ftp = new Ftp(ftpServerIp,port,user,pass);
//上传本地文件
ftp.upload(ftpUploadPath, imageFileName,inputStream);
Console.log("path="+ ftpUploadPath+"//"+imageFileName);
ftpPath.add("ftp://"+ftpServerIp+":"+port+ftpUploadPath+"/"+imageFileName);
}
}
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return ftpPath;
}