public File getFile(String url) throws Exception {
File file = null;
URL urlfile;
InputStream inStream = null;
OutputStream os = null;
try {
file = File.createTempFile("net_url", ".jpg");
urlfile = new URL(url);
inStream = urlfile.openStream();
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
if (null != inStream) {
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
public Integer addPhoto(PhotoEntity photoEntity) {
String orgUrl=null;
if (photoEntity.getPhotoOrgImgurl().contains("oyxe80s4l.bkt.clouddn")) {
orgUrl=photoEntity.getPhotoOrgImgurl().replace("oyxe80s4l.bkt.clouddn","res.ytaxx");
}else{
orgUrl=photoEntity.getPhotoOrgImgurl();
}
BufferedImage sourceImg = null;
File picture=null;
File orgImg = null;
String thumbnailUrl=orgUrl+"?imageView2/2/w/500";
try {
picture = getFile(thumbnailUrl);
orgImg = getFile(photoEntity.getPhotoOrgImgurl());
sourceImg = ImageIO.read(new FileInputStream(picture));
} catch (Exception e) {
throw new RuntimeException("获取图片属性异常");
}
photoEntity.setCreateTime(new Date()).setPhotoDownloads(0).setDeleted(1).setPhotoWidth(sourceImg.getWidth()).setPhotoImgUrl(thumbnailUrl)
.setPhotoHeight(sourceImg.getHeight()).setSize(Integer.valueOf((int) (orgImg.length())));
Integer integer = photoMapper.addPhoto(photoEntity);
materialMapper.updateMateriaCount(photoEntity.getParentId(),"add");
picture.deleteOnExit();
orgImg.deleteOnExit();
return integer;
}