TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
根据图片的url将图片保存到本地。
一、代码
- import java.io.*;
- import java.net.*;
- public class SaveImage {
- public static void main(String args[]) {
- DataInputStream di = null;
- FileOutputStream fo = null;
- byte [] b = new byte[1];
-
- try {
- // input
- URL url = new URL(args[0]);
- URLConnection urlConnection = url.openConnection();
- urlConnection.connect();
- di = new DataInputStream(urlConnection.getInputStream());
- // output
- fo = new FileOutputStream(args[1]);
- // copy the actual file
- // (it would better to use a buffer bigger than this)
- while(-1 != di.read(b,0,1)) {
- fo.write(b,0,1);
- }
- di.close();
- fo.close();
- }
- catch (Exception ex) {
- ex.printStackTrace();
- System.exit(1);
- }
- System.out.println("done.");
- }
- }
- 二、运行方法,如:
- C:java>java SaveImage http://www.java3z.com/cwbwebhome/images/logo.gif logo.gif
- done.
-
复制代码
源码下载:http://file.javaxxz.com/2014/10/2/051406094.zip |
|