|
今天在微博上看到陈能技共享的一批性能测试资料,很不错的,分享链接给同行们。
链接如下:http://www.perftestplus.com/resources/
由于资料很多,手动下载很麻烦,所以特地写了一段自动下载程序,也一并分享之。这段代码大家改吧改吧去下载任何网页上的价格资源都是可以的。
-
- package com.qiang.misc;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
-
- public class PerfTestPDFDownload {
- public static void main(String[] args) {
- PerfTestPDFDownload ptDownload = new PerfTestPDFDownload();
- String response = ptDownload.doRequest("http://www.perftestplus.com/resources/");
- List<String> al = ptDownload.doSearch(response, "<li><a href=\"", "\">");
- for (int i=1; i<al.size(); i++) {
- String pdfSource = "http://www.perftestplus.com/resources/" + al.get(i);
- ptDownload.doDownload(pdfSource, "E:\\51Testing\\LoadRunner\\PerfTestPlus\\" + al.get(i));
- }
- }
-
- public String doRequest(String src) {
- String response = "", line = "";
- try {
- URL url = new URL(src);
- HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
-
- urlConnection.setUseCaches(false);
- urlConnection.connect();
-
- BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "GBK"));
- while ((line = in.readLine())!= null) {
- response += line + "\n";
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return response;
- }
-
- public List<String> doSearch(String source, String left, String right) {
- int posLeft, posRight, tempLeft;
- String tempSource, result;
- List<String> list = new ArrayList<String>();
- int leftLen = left.length();
- int rightLen = right.length();
-
- try {
- source = source.substring(source.indexOf(left), source.lastIndexOf(right)+rightLen+1);
- while (((posLeft = source.indexOf(left)) >= 0) && ((posRight = source.indexOf(right)) >= 0)) {
- tempSource = source.substring(0, posRight);
- tempLeft = tempSource.lastIndexOf(left);
- result = tempSource.substring(tempLeft+leftLen);
- list.add(result);
- source = source.substring(posRight+rightLen);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return list;
- }
-
- public int doDownload(String src, String path) {
- int dlstatus;
- try {
- URL url = new URL(src);
- File outFile = new File(path);
- HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
- urlConnection.setUseCaches(false);
- urlConnection.setReadTimeout(20000);
- urlConnection.connect();
- if (urlConnection.getResponseCode() >= 400)
- dlstatus = urlConnection.getResponseCode();
- else {
- OutputStream os = new FileOutputStream(outFile);
- InputStream is = urlConnection.getInputStream();
- byte[] buff = new byte[1024];
- while(true) {
- int readed = is.read(buff);
- if(readed == -1) {
- break;
- }
- byte[] temp = new byte[readed];
- System.arraycopy(buff, 0, temp, 0, readed);
- os.write(temp);
- }
- is.close();
- os.close();
- dlstatus = 200;
- }
- urlConnection.disconnect();
- }
- catch (Exception e) {
- dlstatus = 999;
- }
- return dlstatus;
- }
- }
复制代码 |
|