java保存网页,使用Java将网页保存到文件

I am trying to read html site using below code,System hanging any hints please:

package com.test;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.net.Socket;

import javax.net.SocketFactory;

import java.net.InetAddress;

public class writingFile {

public static void main(String a[]) throws Exception {

SocketFactory factory=SocketFactory.getDefault();

Socket socket=new Socket(InetAddress.getByName("java.sun.com"), 80);

BufferedWriter out=new BufferedWriter(new FileWriter("C://test.html"));

int data;

while((data=socket.getInputStream().read()) != -1) {

out.write((char)data);

out.flush();

}

}

}

Regards,

Raj

解决方案

This is HTTP. You can't just open a socket and start reading something. You have to be polite to the server and send a request first:

socket.getOutputStream().write("GET /index.html HTTP/1.0\n\n".getBytes());

socket.getOutputStream().flush();

Then read a HTTP response, parse it, and get your html page back.

EDIT I wrote what to do with sockets only because it was the immediate problem of the OP. Using URLConnection is the correct way, as answered by @Mike Deck.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值