java下载网页
❶ 设计一个java程序,下载由URL指定的网页的源代码,找出其中所有的超链接。
importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
{
privateJTextFielrlInput;
privateJTextAreaviewArea;
publicstaticvoidmain(String[]args){
newHttpViewer();
}
publicHttpViewer(){
this.setTitle("HttpViewer");
this.setSize(800,600);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initPanel();
initAction();
this.setVisible(true);
}
//这个方法用来设置窗口布局
privatevoidinitPanel(){
JPanelnorthPanel=newJPanel();
JLabelurlInputLabel=newJLabel("URL:");
urlInput=newJTextField(60);
northPanel.add(urlInputLabel);
northPanel.add(urlInput);
this.add(northPanel,BorderLayout.NORTH);
JPanelcenterPanel=newJPanel();
viewArea=newJTextArea(27,60);
centerPanel.add(newJScrollPane(viewArea));
this.add(centerPanel);
}
//这个方法用来设置事件
privatevoidinitAction(){
urlInput.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
Stringtext=urlInput.getText();
if(text==null||text.length()==0){
viewArea.setText("您没有输入URL");
return;
}
try{
URLurl=newURL(text);
Stringcontext=getContent(url);
if(context!=null){
searchFromText(context);
}
}catch(MalformedURLExceptione1){
viewArea.setText("您输入的URL不合法:"+text);
}
}
});
}
privateStringgetContent(URLurl){
StringBufferbuilder=newStringBuffer();
intresponseCode=-1;
HttpURLConnectioncon=null;
try{
con=(HttpURLConnection)url.openConnection();
con.setRequestProperty("User-Agent",
"Mozilla/4.0(compatible;MSIE5.0;WindowsNT;DigExt)");//IE代理进行下载
con.setConnectTimeout(60000);
con.setReadTimeout(60000);
//获得网页返回信息码
responseCode=con.getResponseCode();
if(responseCode==-1){
viewArea.setText("连接失败:"+url.toString());
returnnull;
}
if(responseCode>=400){
viewArea.setText("请求失败,错误码:"+responseCode);
returnnull;
}
InputStreamis=con.getInputStream();
InputStreamReaderisr=newInputStreamReader(is);
BufferedReaderbr=newBufferedReader(isr);
Stringstr=null;
while((str=br.readLine())!=null)
builder.append(str);
is.close();
}catch(IOExceptione){
e.printStackTrace();
viewArea.setText("IOException:"+url.toString());
}finally{
con.disconnect();
}
returnbuilder.toString();
}
privatevoidsearchFromText(Stringcontext){
viewArea.setText("查找URL中: ");
Patternpattern=Pattern.compile("<a([^>]+)*>(.*?)</a>");
Matchermatcher=pattern.matcher(context);
while(matcher.find()){
for(Stringprop:matcher.group(1).split("")){
intindexOf=prop.indexOf('=');
if(indexOf>0){
if(prop.substring(0,indexOf).equals("href")){
Stringurl2=prop.substring(indexOf+2,prop.length()-1);
viewArea.append(url2+" ");
}
}
}
}
}
}
❷ JAVA编程 如何将下载的网页另存为到本地电脑上
Stream resStream = wc.OpenRead(PageUrl);
StreamReader sr = new StreamReader(resStream,System.Text.Encoding.Default);
ContentHtml.Text = sr.ReadToEnd();
resStream.Close();
❸ 去哪里下载java系统的前端页面
Java文件下载,提供前端页面下载、HttpClient接口下载 学习Java的文件上传/下载需要先明白一下几点: 1、下载的资源,有两种: 1)、本地文件:即项目运行时可访问的文件目录
❹ java在哪下载比较好
可以到sun的官方网站http://www.java.com/zh_CN/下载jre,只用于运行Java程序
如果你是开发人员,可以到http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新版的jdk,网页是英文的,可以用Google翻译或者bing翻译
❺ 在java.com无法进入“所有java下载”页面怎么办
可能是你点错了。换个浏览器可以试试
❻ java如何实现超链接下载
java实现超链接下载方法如下:
response.setHeader("Content-disposition","attachment;filename="下载的文件名字);
备注:让response调用setheader方法添加下载的头给客户的浏览器,浏览器收到该头后就会打开相应的下载对话框。
❼ java代码实现网页下载
看你的意思,我觉得你可以去网上查查java网络爬虫的实现,就是自动抓取网页内容,并保存
http://blog.csdn.net/binyao02123202/article/details/5725396这里有个链接你可以参考下
❽ java中如何实现网页以Excel的形式下载的功能
两种方式:1)将你的网页报表用poi或jxl,转化为excel格式,然后通过servlet的流输出 2)使用现有的报表工具实现,我用finereport,其他的你也可以去Google搜下。
❾ 用java读取网页内容从并下载此网页
URL url=new URL("http://www..com");//取得资源对象
URLConnection uc=url.openConnection();//生成连接对象
uc.setDoOutput(true);
uc.connect(); //发出连接
String temp;
final StringBuffer sb = new StringBuffer();
final BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream(),"gb2312"));
while ((temp = in.readLine()) != null) {
sb.append("\n");
sb.append(temp);
}
in.close();
System.out.println(sb);
❿ 使用java的io流下载了一个网页,但是打开时出现乱码,求助!
你用java的IO读写的时候,记得设置参数里面的编码为下载网页的编码,要一致