Java使用URL读取网络图片资源 并保存在本地
2015-03-31 15:39
176 查看
通过URL,从网络上读取图片,并存入本地,文件名为s.jpg
public class Main { public static void main(String[] args) { System.out.println("Start"); try { URL url = new URL("http://ww4.sinaimg.cn/large/6592c2e0jw1eqnjtyr98ej20f00qot9t.jpg"); InputStream inputStream = url.openStream(); File file = new File("s.jpg"); // FileOutputStream继承自抽象类OutPutStream 实例化了write方法 FileOutputStream fileOutputStream = new FileOutputStream(file); // 接收缓冲 byte[] bytes = new byte[0x400]; int count = inputStream.read(bytes); do { // 从bytes的第0个字节开始,把count个字节写到文件里 fileOutputStream.write(bytes, 0, count); // 把数据从url读出count个字节,保存在bytes count = inputStream.read(bytes); } while (count != -1);// 如果inputStream中的数据全部读完了,则count为-1 inputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("IOException"); } System.out.println("The End"); } }
相关文章推荐
- 使用Java读取网络图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Android 使用URL读取网络图片资源
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地实例
- Java从网络上读取图片并保存至本地
- Java--多线程读取网络图片并保存在本地
- 使用URL读取网络图片资源
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- java读取网络上的资源保存本地
- 【Java】Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- 记录--java获取网络资源(图片、音频等)保存本地
- Java从网络读取图片并保存至本地 .
- java读取网络上的图片并保存到本地,图片文件名不变
- Java从网络读取图片并保存至本地