Java代码优化:将imageUrl20P地址中的图片复制到指定路径
以下是修改后的代码:
if ((images != null) && (cellNum < (displayKeys.length + images.length() - 1))) {
for (int j = 0; j < images.length(); j++) {
String imageUrl = images.optString(j);
String imageUrl20P = imageUrl + '?imageMogr2/thumbnail/!20p';
String[] parts = imageUrl.split('/');
String imageURLName = parts[parts.length - 1];
String path = getfilePath(req, 'upload');
String imageHyperLinkString = '=HYPERLINK('' + imageURLName + '')';
System.out.println(imageUrl);
System.out.println(imageHyperLinkString);
CellUtil.createCell(r, cellNum + j, imageHyperLinkString);
// 将imageUrl20P地址中的图片复制到path中
try {
URL url = new URL(imageUrl20P);
BufferedImage image = ImageIO.read(url);
ImageIO.write(image, 'jpg', new File(path + '/' + imageURLName));
} catch (IOException e) {
e.printStackTrace();
}
// 将imageUrl添加到allImageUrls中
filenamesWithPath += imageURLName + '\n';
}
}
这里使用ImageIO类来读取imageUrl20P对应的图片并将其保存到path中。
原文地址: https://www.cveoy.top/t/topic/qFFQ 著作权归作者所有。请勿转载和采集!