主要思路:
1.读取所有图片,先传到服务器本地,用row行编号命名图片名称.
2.传到图片服务器,获取图片url
3.与excel其它数据行匹配
--------------- 不知为啥首尾两行不进去的分割线 ------------------------------
public void importExcel(MultipartFile files) throws Exception {
InputStream inputStream = files.getInputStream();
ExcelReader excelReader = ExcelUtil.getReader(inputStream);
Workbook workbook = excelReader.getWorkbook();
HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0);
//excel2003
if(workbook instanceof HSSFWorkbook){
}else if (workbook instanceof XSSFWorkbook){
//07的是XSSF,目前只考虑03
//excel 2007
log.info("##### 暂时只支持2003excel");
return;
}
//excel上传图片到本地
List<String> urlList=new ArrayList<>();
for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
if (shape instanceof HSSFPicture) {
HSSFPicture pic = (HSSFPicture) shape;
//获取行编号,放在图片名字里,用于后面与excel其它数据行匹配
int row = anchor.getRow2();
//获取列编号
int col = anchor.getCol2();
log.info("####### row: "+row+" ###### col:"+col);
HSSFPictureData pictureData = pic.getPictureData();
//图片扩展名
String fileSuffix = pictureData.suggestFileExtension();
String url = "//data//test//img//"+ RandomUtil.randomString(10)+"_"+row+"."+fileSuffix;
FileUtil.writeBytes(pictureData.getData(), url);
urlList.add(url);
}
}
//上传图片到fastdfs
Map<String,List> imagemap=new HashMap<>();
for (int i = 0; i < urlList.size(); i++) {
String localimageUrl = urlList.get(i);
log.info("####### 上传的图片地址"+localimageUrl);
File file =new File(localimageUrl);
FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.IMAGE_JPEG.toString(), fileInputStream);
//转为MultipartFile传到fastdfs,也许有更好方法,此方法为fasedfs常用方法,因此不再赘述
StorePath storePath = fastFileStorageClientUtil.uploadFile(multipartFile);
String fullPath = storePath.getFullPath();
log.info("######### fullPath: "+ fullPath);
//获取本地图片row编号与真实图片url整理为map,key为row行号
String[] split = localimageUrl.split("\\.");
String imageName = split[0];
String[] imageNo = imageName.split("_");
String row = imageNo[1];
if(ObjectUtil.isEmpty(imagemap.get(row))){
List a =new ArrayList();
//拼上完整域名,此变量需自己定义
a.add(WebUrl+fullPath);
imagemap.put(row,a);
}else{
List list = imagemap.get(row);
list.add(WebUrl+fullPath);
imagemap.put(row,list);
}
}
log.info("######### imagemap: "+ JSONUtil.toJsonStr(imagemap));
// 读取其它excel数据
List<List<Object>> read = excelReader.read(1, excelReader.getRowCount());
for (int i = 0; i < read.size(); i++) {
//获取excel本行图片列表
List<String> list = imagemap.get(String.valueOf(i+1));
//其它业务代码,根据自己需要实现.....
}
}`
--------------- 不知为啥首尾两行不进去的分割线 ------------------------------
`
标签:03,String,--,excel,图片,new,imagemap,row From: https://www.cnblogs.com/lpcyj/p/18321386