通过⾃⼰技能把某个⽹站的ppt全部下载下来的过程1、该⽹站的ppt链接全部都在页⾯上,⽤正则⼿动提取所有链接,放在指定位置的,以txt形式保存,格式如下
2、写个java⽂件处理⼀下,如下:
1package platform;
2
3import java.io.BufferedInputStream;
4import java.io.BufferedOutputStream;
5import java.io.BufferedReader;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.IOException;
11import java.io.InputStreamReader;
12import java.io.UnsupportedEncodingException;
13import java.HttpURLConnection;
14import java.URL;
15import java.util.HashMap;
16import java.util.Map;
17
18import org.apache.http.HttpResponse;
19import org.apache.http.client.ClientProtocolException;
20import org.apache.hods.HttpPost;
21import org.apache.http.impl.client.DefaultHttpClient;
22
23public class TestQConDownload {
24
25public static void main(String[] args) {
26 BufferedReader bufferedReader;
27 String lineTxt = null;
28 String title="1";
url的一般格式顺序是29 String url="";
30try {
31//读⽂件
32 bufferedReader = readTxtFile("E:\\test\\");
33//循环遍历每⾏
34while((lineTxt = adLine()) != null){
35if(lineTxt.startsWith("【标题】")){
36 title = lineTxt.substring(4).replaceAll(":", "");
37 System.out.println(title);
38 }
39if(lineTxt.startsWith("【下载地址】")){
40 url= lineTxt.substring(6);
41//获取跳转后的地址
42 url = getRedirectLocation(url);
43 System.out.println(url);
44//下载到指定位置
45 downloadFile(url, "E:\\test\\download\\"+title+".pdf");python字典添加元素
46 }
47 }
48 bufferedReader.close();
49 } catch (UnsupportedEncodingException e) {
49 } catch (UnsupportedEncodingException e) {
50// TODO Auto-generated catch block
51 e.printStackTrace();
52 } catch (FileNotFoundException e) {
53// TODO Auto-generated catch block
54 e.printStackTrace();
55 } catch (IOException e) {
56// TODO Auto-generated catch block
57 e.printStackTrace();
58 }
59
60 }
61
62public static String getRedirectLocation(String url) throws ClientProtocolException, IOException {
63 String SEND_MESSAGE_URL = url;
64 Map<String, Object> params = new HashMap<String, Object>();
65 HttpPost get = new HttpPost(SEND_MESSAGE_URL);
66 get.setHeader("Cookie", "dx_un=%E5%B9%B4%E8%BD%BB%E7%9A%84%E7%96%AF%E5%AD%90;
dx_avatar=http%3A%2F%2F7xil0e1.z0.glb.clouddn%2Fuser_580d84f25ea61.png; dx_token=0c6b719ffff50f3746b64f058cb4e719");
67 get.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
68 get.setHeader("Accept-Encoding", "zh-CN,zh;q=0.8");
69 get.setHeader("Connection", "keep-alive");
70 get.setHeader("Host", "");
71 get.setHeader("Referer", "2016.qconshanghai/schedule");
72 get.setHeader("Upgrade-Insecure-Requests", "1");
73 get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36");
74
75// 设置编码
76 HttpResponse re = new DefaultHttpClient().execute(get);
77/*if (re.getStatusLine().getStatusCode() == 200) {// 如果状态码为200,就是正常返回
78 String result = Entity());
79 System.out.println(result);
80 }*/
java下载过程81 String location = re.getFirstHeader("Location").getValue();
82 leaseConnection();
83return location;
84 }
85
86/**
87 * 下载远程⽂件并保存到本地
88 * @param remoteFilePath 远程⽂件路径
89 * @param localFilePath 本地⽂件路径
90*/
91public static void downloadFile(String remoteFilePath, String localFilePath)
92 {
93 URL urlfile = null;
94 HttpURLConnection httpUrl = null;
95 BufferedInputStream bis = null;
96 BufferedOutputStream bos = null;
97 File f = new File(localFilePath);
98try
99 {
100 urlfile = new URL(remoteFilePath);
101 httpUrl = (HttpURLConnection)urlfile.openConnection();
102 t();
103 bis = new InputStream());
104 bos = new BufferedOutputStream(new FileOutputStream(f));
105int len = 2048;
106byte[] b = new byte[len];
107while ((len = ad(b)) != -1)
叭组词100个108 {
软考视频教程网盘109 bos.write(b, 0, len);
110 }
111 bos.flush();
112 bis.close();
112 bis.close();
113 httpUrl.disconnect();
114 }
115catch (Exception e)
116 {
117 e.printStackTrace();
118 }
119finally
120 {
postman造句121try
122 {
123 bis.close();
124 bos.close();
125 }
126catch (IOException e)
127 {
128 e.printStackTrace();
129 }
130 }
131 }
132
133public static BufferedReader readTxtFile(String filePath) throws UnsupportedEncodingException, FileNotFoundException{ 134 String encoding="UTF-8";
135 File file=new File(filePath);
136 InputStreamReader read = new InputStreamReader(
137new FileInputStream(file),encoding);//考虑到编码格式
138 BufferedReader bufferedReader = new BufferedReader(read);
139return bufferedReader;
140 }
141 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论