使用HttpClient和iText下载slideshare上的文档

本文介绍了一种使用Java程序从指定网址抓取图片并将其转换为PDF的方法。主要步骤包括:通过HttpClient获取HTML页面;解析HTML以提取图片数量及首张图片链接;下载所有页面的图片;最后利用iText库将图片合成为PDF文件。
摘要由CSDN通过智能技术生成
1、使用HttpClient获取页面HTML
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();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值