1. 读取网络资源图片流public static void main(String[] args) {
// TODO 自动生成方法存根
//跨过代理服务器连接网络
try {
java.net.InetAddress addr = InetAddress
.getByName("192.168.0.254");
InetSocketAddress sa = new InetSocketAddress(addr, 8080);
// sa.
java.net.Proxy proxy = new java.net.Proxy(Proxy.Type.HTTP, sa);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("csk",
new String("csk").toCharArray());
}
});
URL url = new URL(
"http://61.152.145.58/EM_Quote2006PictureRWS/Default.aspx?StockCode=600900&Type=10");
URLConnection conn = url.openConnection(proxy);
java.io.InputStream imageIn = conn.getInputStream();
java.io.File file = new java.io.File("c:\\aa.png");
java.io.FileOutputStream fos = new java.io.FileOutputStream(
file);
byte[] value = new byte[1024];
int aa=0;
while ((aa=imageIn.read(value)) > 0) {
fos.write(value,0,aa);
}
fos.close();
imageIn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2. 读取网络图片,并对图片进行修改,添加文字处理:
/**
*
*/
package com;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @author shaokun305
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// try {
// JabberServer.createServer();
// System.out.println("=========================================");
// JabberClient.createClient();
// } catch (IOException e) {
// e.printStackTrace();
// }
for (int i = 226; i < 228; i++) {
// int i=3;
String[] jpgPath = getImgPahthStr(i + 1);
// String[]
// jpgPath={"http://cache.orion.sina.com.cn/supportsauto_d/200610bjcarmodel/e0/2c/061119_"+(i+1)+"_1.JPG",
// "http://cache.orion.sina.com.cn/supportsauto_d/200610bjcarmodel/e0/2c/061119_"+(i+1)+"_2.JPG",
// "http://cache.orion.sina.com.cn/supportsauto_d/200610bjcarmodel/e0/2c/061119_"+(i+1)+"_3.JPG",
// "http://cache.orion.sina.com.cn/supportsauto_d/200610bjcarmodel/e0/2c/061119_"+(i+1)+"_4.JPG"
// };//
// http://cache.orion.sina.com.cn/supportsauto_d/200610bjcarmodel/2a/74/061119_204_1.JPG
getImgOut(jpgPath, i + 1);
}
//
}
private static void getImgOut(String[] urlArray, int number) {
try {
java.net.InetAddress addr = InetAddress.getByName("192.168.0.254");
InetSocketAddress sa = new InetSocketAddress(addr, 8080);
// sa.
java.net.Proxy proxy = new java.net.Proxy(Proxy.Type.HTTP, sa);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("csk",
new String("csk").toCharArray());
}
});
if (urlArray != null)
for (int i = 0; i < urlArray.length; i++) {
String strUrl = urlArray;
// URL url=new
// URL("http://cache.orion.sina.com.cn/supportsauto_d/200610bjcarmodel/af/87/061118_4_1.JPG");
URL url = new URL(strUrl);
URLConnection conn = url.openConnection(proxy);
java.io.InputStream imageIn = conn.getInputStream();
JPEGImageDecoder decoder = JPEGCodec
.createJPEGDecoder(imageIn);
BufferedImage image = decoder.decodeAsBufferedImage();
Graphics g = image.getGraphics();
initFont(g); // 初始化字体
g.drawString(number + "号", 20, 20);
// 输出数据流
java.io.File file = new File("d:\\images\\" + number + "_"
+ (i + 1) + ".JPG");
java.io.FileOutputStream output = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec
.createJPEGEncoder(output);
encoder.encode(image);
imageIn.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
private static void initFont(Graphics g) {
String f; // 字体颜色
int fontSize = 16; // 字体大小
String f; // 字体风格(斜体,粗体等)
String f; // 字体名称
g.setColor(Color.RED);
Font mFont = new Font(fontName, Font.PLAIN, fontSize);// 默认字体
if (fontStyle.equalsIgnoreCase("italic"))
mFont = new Font(fontName, Font.ITALIC, fontSize);
if (fontStyle.equalsIgnoreCase("bold"))
mFont = new Font(fontName, Font.BOLD, fontSize);
if (fontStyle.equalsIgnoreCase("plain"))
mFont = new Font(fontName, Font.PLAIN, fontSize);
// System.out.println("字体大小:=" + mFont.getSize());
g.setFont(mFont);
}
/**
*
*/
private static String[] getImgPahthStr(int number) {
try {
java.net.InetAddress addr = InetAddress.getByName("192.168.0.254");
InetSocketAddress sa = new InetSocketAddress(addr, 8080);
// sa.
java.net.Proxy proxy = new java.net.Proxy(Proxy.Type.HTTP, sa);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("csk",
new String("csk").toCharArray());
}
});
URL url = new URL(
"http://supports.auto.sina.com.cn/200610bjcarmodel/show.php?id="
+ number);
URLConnection conn = url.openConnection(proxy);
java.io.InputStream in = conn.getInputStream();
java.io.BufferedReader bin = new BufferedReader(
new java.io.InputStreamReader(in));
if (number <= 202)
return getJpgArrayOne(bin);
else
return getJpgArray(number, bin);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* @param bin
* @return
* @throws IOException
*/
private static String[] getJpgArrayOne(java.io.BufferedReader bin)
throws IOException {
boolean flag = false;
StringBuffer jpgStr = new StringBuffer();
while (true) {
String str = bin.readLine();
if (str.indexOf("var imgs = new Array") != -1)
flag = true;
if (str.indexOf("var imgi = 0;") != -1)
break;
if (flag)
jpgStr.append(str);
// str="";
// Get the entry name; it should be the same as specified on URL
// Get the jar entry
}
bin.close();
String pathStr = "";
pathStr = jpgStr.substring(jpgStr.indexOf("(\"") + 2);
pathStr = pathStr.substring(0, pathStr.indexOf("\")"));
System.out.println(pathStr);
String[] jpgStrArr = pathStr.split("\",\"");
// System.out.println(jpgStrArr[0]);
// System.out.println(jpgStrArr[1]);
// System.out.println(jpgStrArr[2]);
// System.out.println(jpgStrArr[3]);
return jpgStrArr;
}
/**
* @param number
* @param bin
* @param flag
* @return
* @throws IOException
*/
private static String[] getJpgArray(int number, java.io.BufferedReader bin)
throws IOException {
String name1=".JPG";
String name2=".jpg";
String name=name1;
boolean flag = false;
String[] jpgStrArr = new String[4];
while (true) {
String str = bin.readLine();
if (str == null)
break;
int index = 1;
index = str
.indexOf("http://cache.orion.sina.com.cn/supportsauto_d/200610bjcarmodel");
if (index != -1)
flag = true;
if (flag) {
int endIndex = str.indexOf(name) + 4;
if(endIndex==3){
name=name2;
endIndex = str.indexOf(name) + 4;
}
jpgStrArr[0] = str.substring(index, endIndex);
break;
}
}
System.out.println(jpgStrArr[0]);
jpgStrArr[1] = jpgStrArr[0].replaceAll(number + "_1"+name, number
+ "_2"+name);
jpgStrArr[2] = jpgStrArr[0].replaceAll(number + "_1"+name, number
+ "_3"+name);
jpgStrArr[3] = jpgStrArr[0].replaceAll(number + "_1"+name, number
+ "_4"+name);
return jpgStrArr;
}
}
java跨越代理服务器连接URL
最新推荐文章于 2024-05-17 00:36:13 发布