当前位置 : 主页 > 编程语言 > java >

下载图片

来源:互联网 收集:自由互联 发布时间:2021-06-28
Url package hbfu.Url;import java.io.*;import java.io.FileOutputStream;import java.io.IOException;import javax.swing.text.html.ImageView;import okio.*;import okhttp3.*;import okhttp3.Request.Builder;public class Https {public static ImageVie
Url
package hbfu.Url;

import java.io.*;

import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.text.html.ImageView;
import okio.*;
import okhttp3.*;
import okhttp3.Request.Builder;


public class Https {
	public static ImageView imageView;
	private final static String IMAGE_URL = "http://www.hbcf.edu.cn/wwwfile/newsimg/20171103081600020.jpg";
    private static OkHttpClient client;
	public static void main(String[] args) throws IOException{
	        client = new OkHttpClient();
	        final Request request = new Request.Builder()
	                .url(IMAGE_URL)
	                .build();

	        client.newCall(request).enqueue(new Callback() {
	        	public void onFailure(Request request, IOException e) {
	                e.printStackTrace();

	            }

	            @Override
	            public void onResponse(Call call, Response response) throws IOException {
	            	if (!response.isSuccessful()) {
	            		
	                    throw new IOException("服务器端错误: " + response);
	            	}
	            	System.out.println(response.body().byteStream());
	            	byte[] bytes = (byte[]) response.body().bytes();
	            	try{
	            	String URL="D:/ak26.bmp";//创建本地图片URL
	            	File file=new File(URL);
	            	FileOutputStream fos=new FileOutputStream(file);
	            	fos.write(bytes,0,bytes.length);
	            	fos.flush();
	            	fos.close();
	            	//byte2image test = new byte2image(bytes,);
	                //......
	            	}catch(Exception e){
	            		System.out.println("Exception: " + e);
	            		e.printStackTrace();
	            	}
	            }

				@Override
				public void onFailure(Call arg0, IOException arg1) {
					// TODO Auto-generated method stub
					
				}
	        });
	    
	}
	private static Builder url(String imageUrl) {
		// TODO Auto-generated method stub
		return null;
	}
}
上一篇:学生项目
下一篇:pom基本配置
网友评论