?? hurl.java~51~
字號:
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 {
System.out.println("開始連接服務器成功");
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 + -