网络编程

        URL url = new URL(path);  
// GET 必须大写
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// 超时 5秒
conn.setReadTimeout(5 * 1000);
InputStream is = conn.getInputStream();

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1 ) {
bos.write(buffer, 0, len);
}

byte[] data = bos.toByteArray();
File file = new File("zhiling.jpg");

FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();

你可能感兴趣的:(J2SE,编程)