体育资讯网

您现在的位置是:首页 > 分类14 > 正文

分类14

java网页浏览器源码(浏览器运行java代码)

hacker2022-06-12 20:07:25分类1454
本文目录一览:1、java获取网页源码的问题2、

本文目录一览:

java获取网页源码的问题

先知道一个网页的编码方式java网页浏览器源码,是utf-8,还是gbk。

1.HttpURLConnection.getContentType();直接读取,效率高,但有很多时候读不到。只是text/html就完事java网页浏览器源码了,没有charset.

2.使用第三方的HttpClient,执行效率较高。但读取网页头header也只适用部分站,很多网站服务段不设置,结果就读成了null.

3.最没有效率的判断方法就是使用inputStreamReader先把正页的html源码读取出来,之后截取charset后面编码。得到编码之后重新再读取一遍。但是效率很低。

做个总结:

/**

* 取得页面编码

*

* @param url

* @return

*/

public String getCharset(String url) throws Exception {

// log.info("进入读页面的关键词:" + keyword);

String charset = "";

int c;

HttpURLConnection httpurlcon = null;

// log.info("url:"+url);

// log.info("charset:"+charset);

log.info("url:" + url);

URL httpurl = new URL(url);

// System.out.println(url+str);

httpurlcon = (HttpURLConnection) httpurl.openConnection();

// google需要身份

httpurlcon.setRequestProperty("User-agent", "Mozilla/4.0");

charset = httpurlcon.getContentType();

log.info("charset1:" + charset);

// 如果可以找到

if (charset.indexOf("charset=") != -1)

charset = charset.substring(charset.indexOf("charset=")

+ "charset=".length(), charset.length());

// 否则读取response.Header头

else {

charset = this.getContentCharset();

log.info("charset2:" + charset);

}

// 如果charset还是为空,那么直接读网页来截取

if (charset == null) {

charset = this.readPageCharset(url);

log.info("charset31:" + charset);

}

return charset;

}

java中如何根据一个网址获得该网页的源代码?

package test;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpTest {

private String u;

private String encoding;

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

HttpTest client = new HttpTest("", "UTF-8");

client.run();

}

public HttpTest(String u, String encoding) {

this.u = u;

this.encoding = encoding;

}

public void run() throws Exception {

URL url = new URL(u);// 根据链接(字符串格式)java网页浏览器源码,生成一个URL对象

HttpURLConnection urlConnection = (HttpURLConnection) url

.openConnection();// 打开URL

BufferedReader reader = new BufferedReader(new InputStreamReader(

urlConnection.getInputStream(), encoding));// 得到输入流,即获得java网页浏览器源码了网页的内容

String line; // 读取输入流的数据,并显示

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

}

}

根据具体问题类型,进行步骤拆解/原因原理分析/内容拓展等。

具体步骤如下java网页浏览器源码:/导致这种情况的原因主要是……

求一个java图片浏览器的源代码,拜托大家了!!!

java网页浏览器源码你代码java网页浏览器源码,看一下注释java网页浏览器源码,为是防止左右移动时鼠标是水平java网页浏览器源码的移动java网页浏览器源码,所以加了一个范轩,

在二十个象素内,认为是水平的。

你看一下吧

还好你这个是鼠标按住移动,如果只是鼠移动就麻烦了

----------------------------------------

import java.awt.Graphics;

import java.awt.MouseInfo;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.io.File;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.filechooser.FileNameExtensionFilter;

public class App extends JFrame implements MouseListener, ActionListener {

int x = 0;

int y = 0;

File[] selectedFiles = null;

int fileIndex = 0;

int width = 200;

int height = 200;

public App() {

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setSize(400, 300);

setResizable(false);

getContentPane().setLayout(null);

JPanel panel = new ImagePanel();

panel.setBounds(12, 40, 370, 218);

getContentPane().add(panel);

addMouseListener(this);

JButton btnBrowse = new JButton("Browse");

btnBrowse.addActionListener(this);

btnBrowse.setBounds(12, 9, 91, 21);

getContentPane().add(btnBrowse);

setVisible(true);

}

public static void main(String[] args) {

new App();

}

public void actionPerformed(ActionEvent e) {

JFileChooser chooser = new JFileChooser();

chooser.setMultiSelectionEnabled(true);

FileNameExtensionFilter filter = new FileNameExtensionFilter(

"JPG GIF Images", "jpg", "gif");

// 设置文件类型

chooser.setFileFilter(filter);

// 打开选择器面板

int returnVal = chooser.showOpenDialog(this);

if (returnVal == JFileChooser.APPROVE_OPTION) {

selectedFiles = chooser.getSelectedFiles();

repaint();

}

}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

Point point = MouseInfo.getPointerInfo().getLocation();

x = point.x;

y = point.y;

}

public void mouseReleased(MouseEvent e) {

Point point = MouseInfo.getPointerInfo().getLocation();

int thisX = point.x;

int thisY = point.y;

System.out.println("thisX=" + thisX + " " + "thisY=" + thisY);

if ((y - thisY 20 y - thisY 0)

|| (y - thisY 0 y - thisY -20)) {

// Y 在20范围内移动认为是水平移动

if (x thisX) {

// right

if (selectedFiles != null

fileIndex selectedFiles.length - 1) {

fileIndex++;

}

} else {

// left

if (selectedFiles != null fileIndex 0) {

fileIndex--;

}

}

} else {

if (x thisX) {

// 右下

width += 20;

height += 20;

} else {

// 左上

width -= 20;

height -= 20;

}

}

repaint();

}

class ImagePanel extends JPanel {

public void paint(Graphics g) {

super.paint(g);

if (selectedFiles != null) {

ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]

.getPath());

g.drawImage(icon.getImage(), 0, 0, width, height, this);

}

}

}

}

用java做一个简易浏览器

import java.awt.*; 

import java.awt.event.*; 

import java.io.IOException; 

import java.net.*; 

import javax.swing.*; 

import javax.swing.event.HyperlinkEvent; 

import javax.swing.event.HyperlinkListener; 

public class HTTPBrowserDemo extends JFrame { 

        private JLabel jlAddress,jlInfo; 

        private JTextField jtfAddress; 

        private JEditorPane jtpShow; 

        private JPanel panel; 

        private JButton btnGO; 

         

        public static void main(String[] args) { 

                HTTPBrowserDemo hbd=new HTTPBrowserDemo(); 

        } 

        HTTPBrowserDemo(){ 

                 

                jlAddress=new JLabel("地址"); 

                jlInfo=new JLabel(); 

                jtpShow=new JEditorPane(); 

                panel=new JPanel(); 

                jtfAddress=new JTextField(20); 

                btnGO=new JButton("转到"); 

                 

                add(panel,BorderLayout.NORTH); 

                add(jtpShow,BorderLayout.CENTER); 

                add(jlInfo,BorderLayout.SOUTH); 

                 

                panel.add(jlAddress); 

                panel.add(jtfAddress); 

                panel.add(btnGO); 

                 

                setVisible(true); 

                setSize(350, 280); 

                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

                 

                btnGO.addActionListener(new ShowHTMLListener()); 

     jtpShow.setEditable(false); 

                jtpShow.addHyperlinkListener(new MyHyperlinkListener()); 

        } 

        class ShowHTMLListener implements ActionListener{ 

                public void actionPerformed(ActionEvent e){ 

                        String str=jtfAddress.getText(); 

                        try { 

                                if (!str.startsWith("http://")){ 

                                        str="http://"+str; 

                                } 

                                jlInfo.setText("连接中..."); 

                                URL address=new URL(str); 

                                jtpShow.setPage(address); 

                                jlInfo.setText("完成"); 

                        } catch (MalformedURLException e1) { 

                                // TODO Auto-generated catch block 

                                e1.printStackTrace(); 

                        } catch (IOException e2) { 

                                // TODO: handle exception 

                        } 

                } 

        } 

        class MyHyperlinkListener implements HyperlinkListener{ 

                public void hyperlinkUpdate(HyperlinkEvent e) { 

                        if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){ 

                                JEditorPane pane=(JEditorPane)e.getSource(); 

                                try { 

                                        pane.setPage(e.getURL()); 

                                } catch (IOException e1) { 

                                        // TODO Auto-generated catch block 

                                        e1.printStackTrace(); 

                                } 

                        } 

                }         

        } 

}

VB或JAVA抓取网页源代码

乱码问题,可以设置编码解决。。Java一般支持UTF-8 如果不是,你可以多改几个编码看看哪个是中文。你也可以把读取到的字节码显示成二进制看看到底是哪种编码,不同编码很好辨认的。。。比如英文的UTF-8 它是高八位全一样。而GBK中英文只有一个字节

java 得到网页源码为啥跟浏览器查看到的网页源码不一样呢?

从浏览器查看到的源码是页面运行过之后的html静态文件(这个就是最终源码了),并不是开发时候的页面源码。

发表评论

评论列表

  • 性许妄愿(2022-06-13 02:10:51)回复取消回复

    码。得到编码之后重新再读取一遍。但是效率很低。做个总结:/** * 取得页面编码 * * @param url * @return */ public String getCharset(String url) throws Exception { // lo

  • 俗野珞棠(2022-06-13 00:04:17)回复取消回复

    t(); } class ImagePanel extends JPanel { public void paint(Graphics g) { super.paint(g); if (selectedFiles != null) { ImageIcon

  • 酒奴南戈(2022-06-13 04:57:01)回复取消回复

                   } catch (IOException e1) {                                         // TODO Auto-generated cat

  • 惑心俗野(2022-06-12 23:00:50)回复取消回复

          jlAddress=new JLabel("地址");                 jlInfo=new JLabel();              

  • 莣萳绿脊(2022-06-13 03:27:38)回复取消回复

    ,BorderLayout.SOUTH);                                  panel.add(jlAddress);                 panel.add(jtfAddress);                 panel.add(btnGO);