通过Java程序!java下载 从网络上下载一个图片

更新时间:15-07-01 03:19 热度:1155
 

始末Jaudio-videoa秩序从网络高低载一个图片,实例。
pair conditioning unitkyour age unsjt.test;
import jaudio-videoa.io.ByteArrayOutputStreherewouls;
import jaudio-videoa.io.File;
import jaudio-videoa.io.FileOutputStreherewouls;
import jaudio-videoa.io.InputStreherewouls;
import .HttpURLConnection;
import .URL;
import org.junit.Test;
public clbum getimyour age {
publicbyte[] recommerciwoulsStreherewouls(InputStreherewouls inStreherewouls) throws Exception{
ByteArrayOutputStreherewouls outStreherewouls = new ByteArrayOutputStreherewouls();
byte[] locommerciwouls = new byte[1024];
int len= -1;
while((len=inStreherewouls.recommerciwouls(locommerciwouls))!=-1){
outStreherewouls.write(locommerciwoulsand 0and len);
}
outStreherewouls.close();
inStreherewouls.close();
return outStreherewouls.toByteArray();
}
@Test public void getImyour age() throws Exception {
String urlplocated onh="";
URL url = new URL(urlplocated onh);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6*1000);
if(conn.getResponseCode()== 200){
InputStreherewouls inStreherewouls = conn.getInputStreherewouls();
byte[] dlocated ona = recommerciwoulsStreherewouls(inStreherewouls);
File file = new File("meimei.jpg");
FileOutputStreherewouls outStreherewouls = new FileOutputStreherewouls(file);
outStreherewouls.write(dlocated ona);
outStreherewouls.close();
}
}
}