Http通过得到获取数据

  进口java.io.FileOutputStream;   进口java.io.IOException;   进口java.io.InputStream;   进口java.net.HttpURLConnection;   进口java.net.MalformedURLException;   进口java.net.URL;   进口org.apache.http.message.BasicNameValuePair;   公开课HttpUtils {   私有静态字符串URL_PATH=" http://192.168.0.102:8080/myhttp/pro1.png”;   公共HttpUtils () {//TODO自动生成构造函数存根   }   公共静态孔隙saveImageToDisk () {   InputStream InputStream=getInputStream ();   byte [] data=https://www.yisu.com/zixun/new字节[1024];   int len=0;   FileOutputStream FileOutputStream=零;   尝试{   fileOutputStream=new fileOutputStream (“C: \ \ test.png”);   在((len=read(数据))!=1){   fileOutputStream。写(数据、0 len);   }   }捕捉(IOException e) {//TODO自动生成的catch块   e.printStackTrace ();   最后}{   如果(inputStream !=null) {   尝试{   inputStream.close ();   }捕捉(IOException e) {//TODO自动生成的catch块   e.printStackTrace ();   }   }   如果(fileOutputStream !=null) {   尝试{   fileOutputStream.close ();   }捕捉(IOException e) {//TODO自动生成的catch块   e.printStackTrace ();   }   }   }   }/* *   *获得服务器端的数据,以InputStream形式返回   * @return   */公共静态InputStream getInputStream () {   InputStream InputStream=零;   HttpURLConnection HttpURLConnection=零;   尝试{   网址URL=new URL (URL_PATH);   如果(url !=null) {   httpURLConnection=(httpURLConnection) url.openConnection ();//设置连接网络的超时时间   httpURLConnection.setConnectTimeout (3000);   httpURLConnection.setDoInput(真正的);//表示设置本次http请求使用得到的方式请求   httpURLConnection.setRequestMethod(“获得”);   int responseCode=httpURLConnection.getResponseCode ();   如果(responseCode==200) {//从服务器获得一个输入流   inputStream=httpURLConnection.getInputStream ();   }   }   }捕捉(MalformedURLException e) {//TODO自动生成的catch块   e.printStackTrace ();   }捕捉(IOException e) {//TODO自动生成的catch块   e.printStackTrace ();   }   返回inputStream;   }   公共静态void main (String [] args) {//从服务器获得图片保存到本地   saveImageToDisk ();   }   }


Http通过得到获取数据