remotepic.java

来自「采用j2ee架构做的bbs」· Java 代码 · 共 85 行

JAVA
85
字号


package net.icefish.edit;

import java.io.*;
import java.net.*;

public class RemotePic
{

 public String picurl;
 public String savepath;

 public RemotePic()
 {
 }

 public boolean download() throws Exception
 {
     String s;
     String s1;
     s = picurl;
     s1 = savepath;
     URLConnection urlconnection;
     URL url = new URL(s);
     urlconnection = url.openConnection();
     urlconnection.connect();
     HttpURLConnection httpurlconnection = (HttpURLConnection)urlconnection;
     int i = httpurlconnection.getResponseCode();
     if(i != HttpURLConnection.HTTP_OK){
     System.out.println("Connect to " + s + " failed,return code:" + i);
     return false;
     }
     InputStream inputstream;
     FileOutputStream fileoutputstream;
     int j = urlconnection.getContentLength();
     inputstream = urlconnection.getInputStream();
     byte abyte0[] = new byte[1024];
     File file = new File(s1);
     if(!file.exists()){
         file.createNewFile();
         }
     fileoutputstream = new FileOutputStream(file);
     int k = 0;
     if(j < 0)
     {
         while(k > -1) 
         {
             k = inputstream.read(abyte0);
             if(k > 0)
                 fileoutputstream.write(abyte0, 0, k);
         }
       //  break MISSING_BLOCK_LABEL_277;
     }
     int l;
     for(l = 0; l < j && k != -1;)
     {
         k = inputstream.read(abyte0);
         if(k > 0)
         {
             fileoutputstream.write(abyte0, 0, k);
             l += k;
         }
     }

     if(l >= j){
 //        break MISSING_BLOCK_LABEL_276;
     System.out.println("download error");
     inputstream.close();
     fileoutputstream.close();
     file.delete();
     return false;
     }
     fileoutputstream.flush();
     fileoutputstream.close();
     inputstream.close();
//     break MISSING_BLOCK_LABEL_303;
 //    Exception exception;
  //   exception;
 //    exception.printStackTrace();
 //    return false;
     return true;
 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?