URL数据流问题
redv 2003-07-31 04:54:43 下面的代码获取新浪等网页的时候没问题,都是正确的。
可是当我获取:http://52bt.vicp.net:800/bt/index.php这个网页的时候。
得到的数据确实错误的。于是我把字节流打印出来,发现字节流跟实际的相比乱七八遭,为什么?
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
/**
*
* <p>Title: 配合解析html文件以插入到数据库</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Net {
private int blockSize = 1024;
private int offset = 0;
private String charset = "ISO-8859-1";
public void setOffset(int offset) {
this.offset = offset;
}
public void setCharset(String c1) {
charset = c1;
}
public Net() {
}
public void writeToFile(File file) throws MalformedURLException,
FileNotFoundException, IOException {
System.out.println("Writing file " + file);
try {
int b;
FileOutputStream fos = null;
fos = new FileOutputStream(file);
DataInputStream dis = new DataInputStream(myURL.openStream());
String inputLine;
while ( (b = dis.read()) != -1) {
fos.write(b);
}
dis.close();
fos.close();
}
catch (MalformedURLException me) {
throw new MalformedURLException(me.getMessage());
}
catch (FileNotFoundException ex) {
throw new FileNotFoundException(ex.getMessage());
}
catch (IOException ex) {
throw new IOException(ex.getMessage());
}
}
/**
* 从Internet等到网页的源文件
* @return String:source file content
*/
public String getHTML() {
String HTML = new String("");
try {
java.net.URLConnection urlCon = myURL.openConnection();
urlCon.connect();
// DataInputStream dis = new DataInputStream(myURL.openStream());//我也尝试了使用openStream()方法,同样的结果。
InputStream is = urlCon.getInputStream();
int inputLine;
int i = 0;
byte[] b = new byte[blockSize];
// inputLine = dis.read();
// System.out.println(inputLine);//用这句打印出来的第一个字节是31,应该是60(<)才对。
//这里是我的测试过程中的代码
while ( (inputLine = is.read(b)) != -1) {
if(offset != -1 ) {
for (i = 0; i < blockSize; i++) {
b[i] = (byte) ( (int) b[i] + offset);
System.out.print(b[i] + "-");
}break;
}
HTML = HTML.concat(new String(b, charset));
}
is.close();
}
catch (MalformedURLException me) {
System.out.println(me);
}
catch (Exception e) {
System.out.println(e);
}
String ret = HTML;
return ret;
}
/**
* 设置URL
* @param myURL URL
*/
public void setURL(URL myURL) {
this.myURL = myURL;
}
public void setURL(String url) throws java.net.MalformedURLException {
this.myURL = new URL(url);
}
private URL myURL; //The Internet URL
}