我现在也刚学Java不久,偶尔编点小程序。。。

现在写个图片格式转换的小程序,纯属练手,恳请大家批评指正!!!

代码如下:

package com.test.p_w_picpath;

import java.io.ByteArrayOutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Image {

 public static void main(String[] args) throws Exception{
   URL p_w_picpathUrl = new URL(" 为网络图片URL地址
   HttpURLConnection connection = (HttpURLConnection)p_w_picpathUrl.openConnection();
   connection.setConnectTimeout(10*1000);
   connection.setRequestMethod("GET");
   InputStream  instream = connection.getInputStream();
   byte[] p_w_picpathbyte = getByte(instream);
   File file = new File("imge.jpeg"); //会在根目录下创建imge.jpeg 图片,后缀你可以改成.png/.gif/.jpg/... 都行
   FileOutputStream fileStream = new FileOutputStream(file);
   fileStream.write(p_w_picpathbyte);
   fileStream.close();
 }
public static byte[] getByte(InputStream instream) throws IOException{
 ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
 byte[] buffer = new byte[1024];
 int len = 0;
 while((len = instream.read(buffer))!= -1){
  byteOutStream.write(buffer, 0, len);
 }
 byteOutStream.close();
 return byteOutStream.toByteArray();
}
}

注意:运行是确保网络连接正常;因为没有输出显示,所以直接刷新项目就行;XXX为网路图片URL地址才行如下: 

引用URL地址(会抛SocketException)如: