Java抓取文件保存到本地

[ 2006-07-12 12:51:17 | Author: zhenhua ]
Font Size: Large | Medium | Small
可以抓取图片、Flash、电影/视频文件等等,保存到本地目录。多用于采集系统

Test.java

import java.io.*;
import java.net.*;

public class Test {
 public boolean saveUrlAs(String photoUrl, String fileName) {
//此方法只能用于HTTP协议
 try {
 URL url = new URL(photoUrl);
 HttpURLConnection connection = (HttpURLConnection) url.
 openConnection();
 DataInputStream in = new DataInputStream(connection.getInputStream());
 DataOutputStream out = new DataOutputStream(new FileOutputStream(
 fileName));
 byte[] buffer = new byte[4096];
 int count = 0;
 while ((count = in.read(buffer)) > 0) {
 out.write(buffer, 0, count);
 }
 out.close();
 in.close();
 return true;
 } catch (Exception e) {
 return false;
 }
 }

 public String getDocumentAt(String urlString) {
//此方法兼容HTTP和FTP协议
 StringBuffer document = new StringBuffer();
 try {
 URL url = new URL(urlString);
 URLConnection conn = url.openConnection();
 BufferedReader reader = new BufferedReader(new InputStreamReader(
 conn.
 getInputStream()));
 String line = null;
 while ((line = reader.readLine()) != null) {
 document.append(line + "\n");
 }
 reader.close();
 } catch (MalformedURLException e) {
 System.out.println("Unable to connect to URL: " + urlString);
 } catch (IOException e) {
 System.out.println("IOException when connecting to URL: " +
 urlString);
 }
 return document.toString();
 }

 public static void main(String[] args) throws IOException {
 Test test = new Test();
 String photoUrl = "http://ad4.sina.com.cn/200601/12/43932_750450.jpg";
 String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
 String filePath = "d:/";
 boolean flag = test.saveUrlAs(photoUrl, filePath + fileName);
 System.out.println("Run ok!\nGet URL file " + flag);
 }
}

转自:http://blog.sina.com.cn/u/3ee207aa010001s1
作者:54powerman
Comments Feed Comments Feed: http://www.zhenhua.org/feed.asp?q=comment&id=378
UTF-8 Encoding Trackback URL: http://www.zhenhua.org/trackback.asp?id=378

View Mode: Show All | Comments: 0 | Trackbacks: 310 | Toggle Order | Views: 26448
Trackback i wanna buy tramadol
[ i wanna buy tramadol | 2012-11-19 03:22:52 ]
Brand and Generic Drugs, Discount Pharmacy. All information about i wanna buy tramadol in one place: http://digilander.libero.it/dsjoppolo/tramadol/i-wanna-buy-tramadol.html
Trackback aristocort a
[ aristocort a | 2012-11-21 02:38:00 ]
Brand and Generic Drugs, Discount Pharmacy. All information about aristocort a in one place: http://buts.xhost.ro/aristocort/aristocort-a.html
Trackback ghb recipe
[ ghb recipe | 2012-12-01 01:31:04 ]
Brand and Generic Drugs, Discount Pharmacy. All information about ghb recipe in one place: http://next.website.org/rohypnol/ghb-recipe.html
Trackback redneck heroin
[ redneck heroin | 2012-12-30 20:00:37 ]
Brand and Generic Drugs, Discount Pharmacy. All information about redneck heroin in one place: http://digilander.libero.it/tankb0y/oxycodone/redneck-heroin.html
Trackback buy zolpidem online uk
[ buy zolpidem online uk | 2013-01-07 20:45:33 ]
Brand and Generic Drugs, Discount Pharmacy. All information about buy zolpidem online uk in one place: http://digilander.libero.it/lpabclp/zolpidem/buy-zolpidem-online-uk.html
Trackback novi codeine phosphate
[ novi codeine phosphate | 2013-01-10 06:14:02 ]
Brand and Generic Drugs, Discount Pharmacy. All information about novi codeine phosphate in one place: http://w123.kilu.de/codeine-phosphate/novi-codeine-phosphate.html
Trackback trois rivieres condylox
[ trois rivieres condylox | 2013-02-12 21:56:55 ]
Brand and Generic Drugs, Discount Pharmacy. All information about trois rivieres condylox in one place: http://utenti.multimania.it/savyon/condylox/trois-rivieres-condylox.html
Trackback wholesale depot
[ wholesale depot | 2013-02-16 08:09:09 ]
Brand and Generic Drugs, Discount Pharmacy. All information about wholesale depot in one place: http://utenti.multimania.it/savyon/sustanon/wholesale-depot.html
Trackback nolvadex sale
[ nolvadex sale | 2013-03-10 10:55:23 ]
Brand and Generic Drugs, Discount Pharmacy. All information about nolvadex sale in one place: http://perso.wanadoo.es/albert3are/nolvadex/nolvadex-sale.html
Trackback where to order
[ where to order | 2013-03-20 09:16:49 ]
Brand and Generic Drugs, Discount Pharmacy. All information about where to order in one place: http://xhost.kilu.de/rohypnol/where-to-order.html
Trackback oxycodone from china
[ oxycodone from china | 2013-04-18 07:27:16 ]
Brand and Generic Drugs, Discount Pharmacy. All information about oxycodone from china in one place: http://mitglied.multimania.de/ipati/oxycodone/oxycodone-from-china.html
Trackback drugs
[ drugs | 2013-05-17 20:30:02 ]
Brand and Generic Drugs, Discount Pharmacy. All information about drugs in one place: http://hem.passagen.se/teamleif/amphetamine/drugs.html
Trackback metformin drug interactions
[ metformin drug interactions | 2013-06-04 09:14:52 ]
Brand and Generic Drugs, Discount Pharmacy. All information about metformin drug interactions in one place: http://perso.wanadoo.es/raulsalas/glucophage/metformin-drug-interactions.html
Trackback sanofi synthelabo
[ sanofi synthelabo | 2013-06-13 18:54:14 ]
Brand and Generic Drugs, Discount Pharmacy. All information about sanofi synthelabo in one place: http://hem.passagen.se/tvaakomp/rimonabant/sanofi-synthelabo.html
Trackback pepcid vs zantac pregnancy
[ pepcid vs zantac pregnancy | 2013-06-19 03:06:32 ]
Brand and Generic Drugs, Discount Pharmacy. All information about pepcid vs zantac pregnancy in one place: http://perso.wanadoo.es/snunyez/pepcid/pepcid-vs-zantac-pregnancy.html
Trackback abbotsford prilosec
[ abbotsford prilosec | 2013-06-29 11:38:03 ]
Brand and Generic Drugs, Discount Pharmacy. All information about abbotsford prilosec in one place: http://codi.atspace.eu/prilosec/abbotsford-prilosec.html
Trackback buy adipex cod
[ buy adipex cod | 2013-07-04 02:04:35 ]
Brand and Generic Drugs, Discount Pharmacy. All information about buy adipex cod in one place: http://hem.passagen.se/dj_miami_trip/adipex/buy-adipex-cod.html
Trackback atorvastatin with clopidogrel
[ atorvastatin with clopidogrel | 2013-07-13 09:33:25 ]
Brand and Generic Drugs, Discount Pharmacy. All information about atorvastatin with clopidogrel in one place: http://mayerl.altervista.org/atorvastatin/atorvastatin-with-clopidogrel.html
Trackback estradiol treatment
[ estradiol treatment | 2013-07-16 07:53:00 ]
Brand and Generic Drugs, Discount Pharmacy. All information about estradiol treatment in one place: http://aylva.pluto.ro/estradiol/estradiol-treatment.html
Trackback drug information
[ drug information | 2013-07-19 00:57:22 ]
Brand and Generic Drugs, Discount Pharmacy. All information about drug information in one place: http://lellow.altervista.org/mebendazole/drug-information.html

If you feel this site you find this information helpful, please click on the donation, which is voluntary,Thank you.
Post Comment
Smilies
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code