1、使用HttpClient获取页面HTML
2、解析HTML获取最大页数和第一页的图像地址
3、分别抓取各页的图像
4、使用iText将所有页面的图像做成PDF
[color=red]前提是需FQ(PacketiX VPN)[/color]
2、解析HTML获取最大页数和第一页的图像地址
3、分别抓取各页的图像
4、使用iText将所有页面的图像做成PDF
[color=red]前提是需FQ(PacketiX VPN)[/color]
private static final String SD_URL = "http://www.slideshare.net/ProphetsAgency/trends-in-interactive-design-2013";
private static final String OUT_PUT_FOLDER = "C:\\sd\\";
private static final String PDF_NAME = "trends-in-interactive-design-2013.pdf";
public static void main(String[] args) throws Exception {
// Get HTML
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(SD_URL);
HttpResponse res = client.execute(get);
if (res.getEntity() != null) {
String htmldata = EntityUtils.toString(res.getEntity());
int maxpage = 0;
String imgurl = "";
List<String> fileNameList = new ArrayList<String>();
// Get max page
Pattern dataPattern = Pattern.compile("<span>/(.+)</span>", Pattern.CASE_INSENSITIVE);
Matcher tagMatcher = dataPattern.matcher(htmldata);
if (tagMatcher.find()) {
maxpage = Integer.parseInt(tagMatcher.group(1));
}
// Get the first page image's URL
Pattern dataPattern2 = Pattern.compile("background-image:url\\(\\'(.+)\\?.+\\'\\)", Pattern.CASE_INSENSITIVE);
Matcher tagMatcher2 = dataPattern2.matcher(htmldata);
if (tagMatcher2.find()) {
imgurl = tagMatcher2.group(1);
}
System.out.println("maxpage:" + maxpage);
// Get all page images to local
for (int i = 1; i <= maxpage; i++) {
String[] uriArray = imgurl.split("/");
String filename = uriArray[uriArray.length-1];
String[] nameArray = filename.split("-");
filename = nameArray[0] + "-" + i + "-" + nameArray[2];
imgurl = imgurl.substring(0,imgurl.lastIndexOf("/")) + "/" + filename;
getUrlImg(imgurl, OUT_PUT_FOLDER + filename);
System.out.println("imgurl:" + imgurl);
fileNameList.add(filename);
}
// Create PDF
Rectangle rect = new Rectangle(PageSize.B5.rotate());
Document document = new Document(rect);
PdfWriter.getInstance(document, new FileOutputStream(OUT_PUT_FOLDER + PDF_NAME));
document.open();
for (int i = 0; i < fileNameList.size(); i++) {
if (i != 0) {
document.newPage();
}
Image img = Image.getInstance(OUT_PUT_FOLDER + fileNameList.get(i));
img.setAlignment(Image.TOP);
document.add(img);
}
document.close();
}
EntityUtils.consume(res.getEntity());
client.getConnectionManager().shutdown();
}
public static void getUrlImg(String URLName, String target) throws Exception {
URL url = new URL(URLName);
URLConnection urlconn = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
int HttpResult = httpconn.getResponseCode();
if (HttpResult != HttpURLConnection.HTTP_OK) {
System.out.print("fail");
return;
}
BufferedInputStream bis = new BufferedInputStream(urlconn.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
byte[] buffer = new byte[1024];
int num = -1;
while (true) {
num = bis.read(buffer);
if (num == -1) {
bos.flush();
break;
}
bos.flush();
bos.write(buffer, 0, num);
}
bos.close();
bis.close();
}