登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> 程序员学前班[不再更新,只读] >> 主题: java 下载图片 [zt]     [回主站]     [分站链接]
标题
java 下载图片 [zt]
clq
浏览(0) + 2010-01-20 11:56:49 发表 编辑

关键字:

java 下载图片


clq
2010-1-20 11:57:12 发表 编辑

SOURCE CODE: getImages.java

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

public class getImages {

public static void getImageURLs(String input1, String input2) throws Exception
{
try {

int src=0, secondQuote=0;
byte byteArray[] = new byte[4082];
String temp = new String();
String baseURL = "http://www.mrjameswright.com/",
htmlFile="index.php",
url = baseURL + htmlFile,
proxy = "proxy.yourcompany.org",
port = "8080",
resetBaseURL = baseURL;

if (!(input1.indexOf("http://") == -1 || input2.equals("")))
{
baseURL = input1;
htmlFile = input2;
url = baseURL + htmlFile;
} else { System.out.println("Invalid input!!!"); return; }

URL server = new URL(url);
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);

HttpURLConnection connection = (HttpURLConnection)server.openConnection();

// You might need to spoof some settings: browser (user-agent), referer, and operating system
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)");
connection.setRequestProperty("Referer","http://www.google.com");
connection.setRequestProperty("ua-os", "Windows XP");

connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();

System.out.println();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;

while ((line = in.readLine()) != null) {
//System.out.println(line);

while (line.indexOf("src=") > -1)
{
src= line.indexOf("src=");
secondQuote= line.indexOf('"', line.indexOf('"', src)+1);
if (line.indexOf("http://", src) == src+5) baseURL = "";
else {
if (line.indexOf("/") == src+5) src++;

baseURL = resetBaseURL;
} // end of else

//System.out.println(line);
//System.out.println(line.indexOf("http://",src));
//System.out.println("SRC: " +src+" 2ndQ: "+secondQuote+ " "+baseURL+line.substring(src+5,secondQuote));
downloadImage(baseURL+line.substring(src+5,secondQuote));


line = line.substring(secondQuote, line.length());


} // end of while loop

} // end of while loop

in.close();

}
catch ( IOException e ) { e.printStackTrace(); }
} // end of getImageURLs

public static void downloadImage(String address)
{
String localFileName = address.substring(address.lastIndexOf("/")+1);

OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
URL url = new URL(address);

Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost","proxy.yourcompany.org");
systemProperties.setProperty("http.proxyPort","8080");


out = new BufferedOutputStream(
new FileOutputStream(localFileName));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
numWritten += numRead;
}
System.out.println(localFileName + "\t" + numWritten);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
}

public static void main(String[] args) {
try{
getImageURLs(args[0],args[1]);
} catch (Exception e) { System.out.println("An error has occurred!!!"); }
} // End of main
}


EXAMPLE COMPILE AND RUN:

C:\>javac getImages.java

C:\>java getImages http://www.mrjameswright.com/ pics.php

show_ads.js      7080
paypald.gif      857
logo_mrjw.gif    1179
shoponline.gif   2151
TopNav.js        5502
menu_com.js      22962
karDesk.jpg      12186
karDesk.jpg      12186
demonhunter.jpg 29451
demonhunter.jpg 29451
swe.jpg 64249
swe.jpg 64249
AmandaK.jpg      29432
AmandaK.jpg      29432
sixflag.jpg      34671
sixflag.jpg      34671
goat.jpg         27268
goat.jpg         27268
ORNL.jpg         23542
ORNL.jpg         23542
escort.jpg       16411
escort.jpg       16411
karo2.jpg        15152
karo2.jpg        15152
JamesK.jpg       30048
JamesK.jpg       30048
jamesPurple.jpg 16716
jamesPurple.jpg 16716
kendrick.jpg     10760
kendrick.jpg     10760
horse.jpg        32182
horse.jpg        32182
Mr_Bunny.jpg     22599
Mr_Bunny.jpg     22599
curtis.jpg       19158
curtis.jpg       19158
jamesskytel2.gif         38485

C:\>


总数:1 页次:1/1 首页 尾页  
总数:1 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1