package chapter02;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.Buffer;
import javax.imageio.ImageIO;
import sun.io.CharToByteConverter;
public class tt {
private String formatName="";
private String imgFileUrl="";
private boolean saveImage(String remoteURL,String localURL){//保存图片
boolean isOK=false;
try {
URL url = new URL(remoteURL);
File testfile = new File(localURL);
//如果没有image文件夹,则创建多层文件夹
String imgPath=localURL.substring(0,localURL.lastIndexOf("\\"));
File imgFile=new File(imgPath);
imgFile.mkdirs();
BufferedImage srcImage = ImageIO.read(url);
String formatName = "JPEG";
if(srcImage!=null){
ImageIO.write(srcImage, formatName, testfile);
System.out.println("OK");
isOK=true;
}
} catch (Exception e) {}
return isOK;
}
public static void main(String[] args) {
tt test2=new tt();
try {
String xxxPath="http://10.252.10.20:8080/hello/abcd.jpg";
String localURL="d:\\image\\abcd.jpg";
test2.saveImage(xxxPath,localURL);
} catch (Exception e) {
e.printStackTrace();
}
}
}