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

}
...全文
82 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hodex 2003-07-31
  • 打赏
  • 举报
回复
在俺机子上连http://www.sina.com.cn都不可用,连不上???
只能连上自己建的服务器,
yumen O!!!
bernieli 2003-07-31
  • 打赏
  • 举报
回复
是不是跟编码方式有关系呢。
jouny0 2003-07-31
  • 打赏
  • 举报
回复
up
redv 2003-07-31
  • 打赏
  • 举报
回复
这个是我正在使用的代码,也是有同样的问题。
就是读取那个网站的数据不行,别的网站都可以。

package com.redv.projects.bittorrent;

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 {
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 {
DataInputStream dis = new DataInputStream(myURL.openStream());
String inputLine;
while ( (inputLine = dis.readLine()) != null) {
HTML = HTML.concat(inputLine);
HTML = HTML.concat("\n");
}
dis.close();
}
catch (MalformedURLException me) {
System.out.println(me);
}
catch (Exception e) {
System.out.println(e);
}
String ret = HTML;
try {
ret = new String(HTML.getBytes("ISO-8859-1"), "GBK");
}
catch (java.io.UnsupportedEncodingException ex) {}
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);
}

public URL getURL() {
return this.myURL;
}

private URL myURL; //The Internet URL

public static void main(String[] args) throws Exception{
Net net = new Net();
net.setURL("http://www.cumt.edu.cn");
net.writeToFile(new File("C:\\mytest.html"));//正常的输出
System.out.println(net.getHTML());
net.setURL("http://52bt.vicp.net:800/bt/");
net.writeToFile(new File("C:\\mytest2.html"));//不正常的输出
System.out.println(net.getHTML());
}

}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧