public class HttpRequestImage {
public static String headpath="/sdcard/head/";//图片保存地址
// public HttpRequestImage()
// {
// }
public static String requestImage(String imageurl,String name) throws Exception
{
InputStream inputstream=null;
String filename=headpath+name+".jpg";
URL imageUrl = new URL(imageurl);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(7000);
int code=conn.getResponseCode();
if(code==200)
{
inputstream = conn.getInputStream();
File file = new File(filename);
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
byte[] buf = new byte[1024];
int length = 0;
while((length = inputstream.read(buf, 0, 1024))!=-1){
raf.write(buf,0,length);
}
inputstream.close();
raf.close();
return filename;
}
else
{
return "";
}
}
}
android图片下载工具类
最新推荐文章于 2022-11-07 14:28:52 发布