⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hurl.java~50~

📁 jwap 协议 udp 可以用于手机通讯
💻 JAVA~50~
字号:
package net.sourceforge.jwap.http;

import java.net.Socket;
import java.io.DataOutputStream;
import java.util.Properties;
import java.net.URL;
import net.sourceforge.jwap.util.Logger;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.net.URLConnection;
import java.net.InetAddress;
import java.io.IOException;
import java.net.ProtocolException;
import java.io.InputStream;
import net.sourceforge.jwap.wtp.markflg;

/**
 * <p>Title: </p>
 *
 * <p>Description: nbpt</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author chen
 * @version 1.0
 */
public class Hurl implements Runnable {
    static Logger logger = Logger.getLogger(HSocket.class);
    URLConnection con = null;

    public InputStream Tcpinput = null;
    protected URL target = null;
    private int responseCode = -1;
    private String responseMessage = "";
    private String serverVersion = "";
    protected ByteArrayInputStream byteStream;
    public String baseheader;
    private Properties header = new Properties();
    private Thread runner = null;
    public int get_post = 0;
    private HttpUpperLayer upperLayer;
    public String geturl = "";
    public int icount = 1;

    public Hurl() {
    }

    public Hurl(HttpUpperLayer m_upperLayer, String url) {
        try {
            upperLayer = m_upperLayer;
            target = new URL(url);
        } catch (Exception ex) {

        }

    }

    public static void main(String[] args) {
        Hurl hurl = new Hurl();
    }

    public int GET(String url) {
        // streamcreat();
        try {
        System.out.println("开始连接服务器");
            con = target.openConnection();

            con.setUseCaches(false);
            con.setDoOutput(true);
            con.setDoInput(true);
            String encod = con.getContentEncoding();

            int len = con.getContentLength();
            if (len == -1) {
                System.out.println("文件不存在");
                responseCode = -1;
            } else {
                Tcpinput = con.getInputStream();
                int i = len;
                // 获得正文数据
                byte databuf[] = new byte[1024];
                byte data[] = new byte[1024];
                int count = 0;
                int word = 0;
                while (((word = Tcpinput.read(databuf)) != -1)) {
                    //=======================================================================
                    if (word > 0) {
                        if (icount == 1) {
                            System.arraycopy(databuf, 0, data, 0, word);
                            System.out.println(icount + " 接收到 "+word+" 字节");
                            icount++;
                        } else {
                            int leng = data.length;
                            byte temp[] = new byte[data.length + word];

                            System.out.println(icount + " 接收到 "+word+" 字节");
                            icount++;
                            System.arraycopy(data, 0, temp, 0, data.length);
                            System.arraycopy(databuf, 0, temp, data.length,
                                             word);
                            data = temp;

                        }
                        markflg.ReceiveBag();

                        //data = temp;//addCapacitybuf(data, databuf);
                        count = data.length;
                        if (count >= len) {
                            responseCode=200;
                            break;
                        }
                    }
                }
                //==================================================================================
                byteStream = new ByteArrayInputStream(data, 0, count);
            }

        } catch (ProtocolException p) {
            String str = "下载失败";
            byteStream = new ByteArrayInputStream(str.getBytes());
            p.printStackTrace();
        } catch (IOException ex) {
            String str = "下载失败";
            byteStream = new ByteArrayInputStream(str.getBytes());
            System.out.println(ex.getMessage());
        }

        if(Tcpinput!=null)
        {
            try {
                Tcpinput.close();
            } catch (IOException ex1) {
            }
            Tcpinput=null;
        }

        return responseCode;
    }

    private byte[] addCapacitybuf(byte rece[], byte dat[]) {
        byte temp[] = new byte[rece.length + 1024];
        temp = rece;
        System.out.println(icount + " 接收到 1024 字节");
        icount++;
        System.arraycopy(rece, 0, temp, 0, rece.length);
        System.arraycopy(dat, 0, temp, rece.length, 1024);
        markflg.ReceiveBag();
        return temp;
    }


    public void start() {
        runner = new Thread(this);
        runner.start();
    }


    public void stop() {
        if (runner != null) {
            runner.stop();
            runner = null;
        }
    }

    public void run() {

        int i = 0;
        int ret = -1;
        while (true) {
            if (get_post == 1) {

                //ret = POST(geturl);
                if (ret == -1) {
                    runner = null;
                    break;
                } else {

                    break;

                }

            } else {

                ret = GET(geturl);

                if (ret == -1) {
                    i++;

                   // break;
                } else {
                    int len = byteStream.available();

                    byte outdata[] = new byte[len + 1];
                    try {
                        byteStream.read(outdata);
                    } catch (IOException ex) {
                        ret = -1;

                    }

                    upperLayer.tr_result(ret, outdata);
                    runner = null;
                    break;
                    //i++;
                }

                if(i>2)
                {
                   runner = null;
                   String backmsg = "下载失败";
                   upperLayer.tr_result(ret, backmsg.getBytes());

                  break;
                }
            }

        }

    }

}

⌨️ 快捷键说明

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