down load pic
2019-04-15 14:32发布
生成海报
-
package imageView;
-
import java.io.ByteArrayOutputStream;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.InputStream;
-
import java.net.HttpURLConnection;
-
import java.net.URL;
-
-
-
-
-
-
-
public class GetImage {
-
-
-
-
-
public static void main(String[] args) {
-
String url = "http://www.baidu.com/img/baidu_sylogo1.gif";
-
byte[] btImg = getImageFromNetByUrl(url);
-
if(null != btImg && btImg.length > 0){
-
System.out.println("读取到:" + btImg.length + " 字节");
-
String fileName = "百度.gif";
-
writeImageToDisk(btImg, fileName);
-
}else{
-
System.out.println("没有从该连接获得内容");
-
}
-
}
-
-
-
-
-
-
public static void writeImageToDisk(byte[] img, String fileName){
-
try {
-
File file = new File("C:\" + fileName);
-
FileOutputStream fops = new FileOutputStream(file);
-
fops.write(img);
-
fops.flush();
-
fops.close();
-
System.out.println("图片已经写入到C盘");
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
-
-
-
public static byte[] getImageFromNetByUrl(String strUrl){
-
try {
-
URL url = new URL(strUrl);
-
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
-
conn.setRequestMethod("GET");
-
conn.setConnectTimeout(5 * 1000);
-
InputStream inStream = conn.getInputStream();
-
byte[] btImg = readInputStream(inStream);
-
return btImg;
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return null;
-
}
-
-
-
-
-
-
-
public static byte[] readInputStream(InputStream inStream) throws Exception{
-
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-
byte[] buffer = new byte[1024];
-
int len = 0;
-
while( (len=inStream.read(buffer)) != -1 ){
-
outStream.write(buffer, 0, len);
-
}
-
inStream.close();
-
return outStream.toByteArray();
-
}
-
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮