菜鸟问题,从网上下载xml为什么总是下不全?

LoocalVinci 2008-09-26 12:33:19
我通过某个url从网上抓取xml文件存到本地,发现xml文件总是只能抓到一部分。请高人指点一二。

系统输出流输出信息如下:
----------
getContentType: text/html; charset=gb2312
getContentLength: -1
getContentEncoding: null
getDate: 1222359162000
getExpiration: 0
getLastModifed: 0
----------

代码如下



import java.util.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;

/**
This program connects to an URL and displays the
response header data and the first 10 lines of the
requested data.

Supply the URL on the parameter.
*/
public class URLCollect
{
public URLCollect(String urlName, String archieve)
{
try
{
URL url = new URL(urlName);
URLConnection connection = url.openConnection();
String content = "";
String curLine = "";

connection.connect();

// print header fields

Map<String, List<String>> headers = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headers.entrySet())
{
String key = entry.getKey();
for (String value : entry.getValue())
System.out.println(key + ": " + value);
}

// print convenience functions

System.out.println("----------");
System.out.println("getContentType: " + connection.getContentType());
System.out.println("getContentLength: " + connection.getContentLength());
System.out.println("getContentEncoding: " + connection.getContentEncoding());
System.out.println("getDate: " + connection.getDate());
System.out.println("getExpiration: " + connection.getExpiration());
System.out.println("getLastModifed: " + connection.getLastModified());
System.out.println("----------");

InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));


// print first ten lines of contents

PrintWriter out = new PrintWriter(new FileWriter(archieve));

while((curLine = reader.readLine())!=null)
{
content = content + curLine + "\r\n";
//System.out.printf(curLine+"\r\n");
}
is.close();
//System.out.printf(content);
out.println(content);
}
catch (IOException e)
{
e.printStackTrace();
}
}

/**
Computes the Base64 encoding of a string
@param s a string
@return the Base 64 encoding of s
*/
public static String base64Encode(String s)
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
Base64OutputStream out = new Base64OutputStream(bOut);
try
{
out.write(s.getBytes());
out.flush();
}
catch (IOException e)
{
}
return bOut.toString();
}
}

/**
This stream filter converts a stream of bytes to their
Base64 encoding.

Base64 encoding encodes 3 bytes into 4 characters.
|11111122|22223333|33444444|
Each set of 6 bits is encoded according to the
toBase64 map. If the number of input bytes is not
a multiple of 3, then the last group of 4 characters
is padded with one or two = signs. Each output line
is at most 76 characters.
*/
class Base64OutputStream extends FilterOutputStream
{
/**
Constructs the stream filter
@param out the stream to filter
*/
public Base64OutputStream(OutputStream out)
{
super(out);
}

public void write(int c) throws IOException
{
inbuf[i] = c;
i++;
if (i == 3)
{
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]);
super.write(toBase64[inbuf[2] & 0x3F]);
col += 4;
i = 0;
if (col >= 76)
{
super.write('\n');
col = 0;
}
}
}

public void flush() throws IOException
{
if (i == 1)
{
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[(inbuf[0] & 0x03) << 4]);
super.write('=');
super.write('=');
}
else if (i == 2)
{
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
super.write('=');
}
}

private static char[] toBase64 =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};

private int col = 0;
private int i = 0;
private int[] inbuf = new int[3];
}
...全文
40 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeah920 2008-09-26
  • 打赏
  • 举报
回复
不会.帮顶.

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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