```/**
* 获取图片保存后的路径
* @param sheet
* @param request
* @return
*/
public static Map<String, String> getPictures(XSSFSheet sheet,HttpServletRequest request){
Map<String, String> map = new HashMap<>();
List<POIXMLDocumentPart> list = sheet.getRelations();
for(POIXMLDocumentPart part:list){
if(part instanceof XSSFDrawing){
XSSFDrawing drawing = (XSSFDrawing) part;
List<XSSFShape> shapes = drawing.getShapes();
for(XSSFShape shape:shapes){
XSSFPicture picture = (XSSFPicture) shape;
XSSFClientAnchor anchor = picture.getPreferredSize();
CTMarker marker = anchor.getFrom();
//获取图片所在行列
String key = marker.getRow() + "-" + marker.getCol();
//上图图片至服务器,返回路径
String path = uploadPicture(key,picture.getPictureData(),request);
map.put(key,path);
}
}
}
return map;
}
/**
* 保存图片至服务器
* @param str
* @param pictureData
* @param request
* @return
*/
public static String uploadPicture(String str,PictureData pictureData,HttpServletRequest request){
//获取服务器路径
String savePath = request.getSession().getServletContext().getRealPath("");
String dateDir = new SimpleDateFormat("yyMMdd").format(new Date());
// 获取图片格式
String ext = pictureData.suggestFileExtension();
//设置保存目录
String uuid = UUID.randomUUID().toString().replace("-", "");
String fileName = uuid + str.replace("-","");
File fileDir = new File(savePath + PIC_PATH + dateDir);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//设置保存路径
String filePath = savePath + PIC_PATH + dateDir + File.separator +fileName + "." + ext;
//设置生成缩略图路径
String smallFilePath = savePath + PIC_PATH + dateDir + File.separator +fileName + "_80_80" + "." + ext;
//将图片转为字节数组
byte[] data = pictureData.getData();
try {
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
stream.write(data);
stream.close();
//生成缩略图方法
createThumbnails(filePath, 80, 80, smallFilePath);
} catch (Exception e) {
e.printStackTrace();
}
String relativePath = PIC_PATH + dateDir + File.separator +fileName + "." + ext;
return relativePath;
}
}
带有图片的Excel信息导入
最新推荐文章于 2024-10-16 08:06:10 发布