public String downloadImage(String imageUrl) throws IOException { String result = ""; if (jcUser.isBackupThumbnail()) { // download thumbnail image byte[] thumbnailImage = NetUtil.readImage(imageUrl); File thumbnailDirectory = new File(path, "thumbnail"); DataUtil.writeImage( thumbnailDirectory, imageUrl.substring(imageUrl.lastIndexOf("/"), imageUrl.length()), thumbnailImage); LOG.debug("saved thumbnail image:" + imageUrl); result = imageUrl.substring(imageUrl.lastIndexOf("thumbnail"), imageUrl.length()); thumbnailCount++; } if (jcUser.isBackupLarge()) { // download large image String largeImageUrl = imageUrl.replaceFirst("thumbnail", "large"); byte[] largeImage = NetUtil.readImage(largeImageUrl); File largeDirectory = new File(path, "large"); DataUtil.writeImage( largeDirectory, largeImageUrl.substring(largeImageUrl.lastIndexOf("/"), largeImageUrl.length()), largeImage); result = imageUrl.substring(imageUrl.lastIndexOf("thumbnail"), imageUrl.length()); LOG.debug("saved large image:" + largeImageUrl); largeCount++; } return result; }
public Status downloadImage(Status s) { if (s.getRetweetedStatus() != null) { s.setRetweetedStatus(downloadImage(s.getRetweetedStatus())); } if (s.getPicUrls() == null || s.getPicUrls().length == 0) return s; Map<Integer, String> failUrls = new HashMap<Integer, String>(); // String[] imageUrls = new String[s.getPicUrls().length]; String[] imageUrls = s.getPicUrls(); // int index = 0; for (int i = 0; i < imageUrls.length; i++) { // for (String imageUrl : s.getPicUrls()) { try { String imageUrl = downloadImage(imageUrls[i]); imageUrls[i] = imageUrl; } catch (IOException e) { LOG.error("occured exception when download images:" + imageUrls[i] + e); LOG.error("keep it,then i will download it again."); failUrls.put(i, imageUrls[i]); } } if (failUrls.size() > 0) { for (int i : failUrls.keySet()) try { LOG.debug("the second time to download image:" + failUrls.get(i)); String imageUrl = downloadImage(failUrls.get(i)); imageUrls[i] = imageUrl; } catch (IOException e) { LOG.error( "occured exception when the second time download images:" + failUrls.get(i) + " \n" + e); imageUrls[i] = "images/fail.thumbnail.jpg"; } } if (jcUser.isBackupThumbnail()) s.setPicUrls(imageUrls); // else //without any picture // s.setPicUrls(null); return s; }