打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
htmlparser 编码问题 - Loiy - ITeye技术网站
有时候,在抓取网站的时候,网站的编码方式可能不统一,这样的情况,可能有些网页编码不成功,而htmlparser报了错,不能正常的读取。抛出来的异常为:org.htmlparser.util.EncodingChangeException: character mismatch (new: 中 [0x4e2d] != old:  [0xd6?]) for encoding change from ISO-8859-1 to GB2312 at character offset 23

为了解决不管它是用何种编码方式,都能够正常读取数据。我在htmlparser的Page类中加了一个字段,之所以要在Page类中加,那是因为它历遍所有的标签过程中,把meta标签属性content捕获到了,并已经传值到setEncoding(String charset)办法中。

          代码如下:

 

Java代码
 
  1. // Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.   
  2. // Jad home page: http://www.kpdus.com/jad.html   
  3. // Decompiler options: packimports(3)    
  4. // Source File Name:   Page.java   
  5.   
  6. package org.htmlparser.lexer;   
  7.   
  8. import java.io.*;   
  9. import java.lang.reflect.InvocationTargetException;   
  10. import java.lang.reflect.Method;   
  11. import java.net.*;   
  12. import java.util.zip.*;   
  13. import org.htmlparser.http.ConnectionManager;   
  14. import org.htmlparser.util.ParserException;   
  15.   
  16. // Referenced classes of package org.htmlparser.lexer:   
  17. //            InputStreamSource, PageIndex, StringSource, Cursor,    
  18. //            Stream, Source   
  19.   
  20. public class Page   
  21.     implements Serializable   
  22. {   
  23.   
  24.     public Page()   
  25.     {   
  26.         this("");   
  27.     }   
  28.   
  29.     public Page(URLConnection connection)   
  30.         throws ParserException   
  31.     {   
  32.         if(null == connection)   
  33.         {   
  34.             throw new IllegalArgumentException("connection cannot be null");   
  35.         } else  
  36.         {   
  37.             setConnection(connection);   
  38.             mBaseUrl = null;   
  39.             return;   
  40.         }   
  41.     }   
  42.   
  43.     public Page(InputStream stream, String charset)   
  44.         throws UnsupportedEncodingException   
  45.     {   
  46.         if(null == stream)   
  47.             throw new IllegalArgumentException("stream cannot be null");   
  48.         if(null == charset)   
  49.             charset = "ISO-8859-1";   
  50.         mSource = new InputStreamSource(stream, charset);   
  51.         mIndex = new PageIndex(this);   
  52.         mConnection = null;   
  53.         mUrl = null;   
  54.         mBaseUrl = null;   
  55.     }   
  56.   
  57.     public Page(String text, String charset)   
  58.     {   
  59.         if(null == text)   
  60.             throw new IllegalArgumentException("text cannot be null");   
  61.         if(null == charset)   
  62.             charset = "ISO-8859-1";   
  63.         mSource = new StringSource(text, charset);   
  64.         mIndex = new PageIndex(this);   
  65.         mConnection = null;   
  66.         mUrl = null;   
  67.         mBaseUrl = null;   
  68.     }   
  69.   
  70.     public Page(String text)   
  71.     {   
  72.         this(text, null);   
  73.     }   
  74.   
  75.     public Page(Source source)   
  76.     {   
  77.         if(null == source)   
  78.         {   
  79.             throw new IllegalArgumentException("source cannot be null");   
  80.         } else  
  81.         {   
  82.             mSource = source;   
  83.             mIndex = new PageIndex(this);   
  84.             mConnection = null;   
  85.             mUrl = null;   
  86.             mBaseUrl = null;   
  87.             return;   
  88.         }   
  89.     }   
  90.   
  91.     public static ConnectionManager getConnectionManager()   
  92.     {   
  93.         return mConnectionManager;   
  94.     }   
  95.   
  96.     public static void setConnectionManager(ConnectionManager manager)   
  97.     {   
  98.         mConnectionManager = manager;   
  99.     }   
  100.   
  101.     public String getCharset(String content)   
  102.     {   
  103.         String CHARSET_STRING = "charset";   
  104.         String ret;   
  105.         if(null == mSource)   
  106.             ret = "ISO-8859-1";   
  107.         else  
  108.             ret = mSource.getEncoding();   
  109.         if(null != content)   
  110.         {   
  111.             int index = content.indexOf("charset");   
  112.             if(index != -1)   
  113.             {   
  114.                 content = content.substring(index + "charset".length()).trim();   
  115.                 if(content.startsWith("="))   
  116.                 {   
  117.                     content = content.substring(1).trim();   
  118.                     index = content.indexOf(";");   
  119.                     if(index != -1)   
  120.                         content = content.substring(0, index);   
  121.                     if(content.startsWith("\"") && content.endsWith("\"") && 1 < content.length())   
  122.                         content = content.substring(1, content.length() - 1);   
  123.                     if(content.startsWith("'") && content.endsWith("'") && 1 < content.length())   
  124.                         content = content.substring(1, content.length() - 1);   
  125.                     ret = findCharset(content, ret);   
  126.                 }   
  127.             }   
  128.         }   
  129.         return ret;   
  130.     }   
  131.   
  132.     public static String findCharset(String name, String fallback)   
  133.     {   
  134.         String ret;   
  135.         try  
  136.         {   
  137.             Class cls = Class.forName("java.nio.charset.Charset");   
  138.             Method method = cls.getMethod("forName"new Class[] {   
  139.                 java.lang.String.class  
  140.             });   
  141.             Object object = method.invoke(nullnew Object[] {   
  142.                 name   
  143.             });   
  144.             method = cls.getMethod("name"new Class[0]);   
  145.             object = method.invoke(object, new Object[0]);   
  146.             ret = (String)object;   
  147.         }   
  148.         catch(ClassNotFoundException cnfe)   
  149.         {   
  150.             ret = name;   
  151.         }   
  152.         catch(NoSuchMethodException nsme)   
  153.         {   
  154.             ret = name;   
  155.         }   
  156.         catch(IllegalAccessException ia)   
  157.         {   
  158.             ret = name;   
  159.         }   
  160.         catch(InvocationTargetException ita)   
  161.         {   
  162.             ret = fallback;   
  163.             System.out.println("unable to determine cannonical charset name for " + name + " - using " + fallback);   
  164.         }   
  165.         return ret;   
  166.     }   
  167.   
  168.     private void writeObject(ObjectOutputStream out)   
  169.         throws IOException   
  170.     {   
  171.         if(null != getConnection())   
  172.         {   
  173.             out.writeBoolean(true);   
  174.             out.writeInt(mSource.offset());   
  175.             String href = getUrl();   
  176.             out.writeObject(href);   
  177.             setUrl(getConnection().getURL().toExternalForm());   
  178.             Source source = getSource();   
  179.             mSource = null;   
  180.             PageIndex index = mIndex;   
  181.             mIndex = null;   
  182.             out.defaultWriteObject();   
  183.             mSource = source;   
  184.             mIndex = index;   
  185.         } else  
  186.         {   
  187.             out.writeBoolean(false);   
  188.             String href = getUrl();   
  189.             out.writeObject(href);   
  190.             setUrl(null);   
  191.             out.defaultWriteObject();   
  192.             setUrl(href);   
  193.         }   
  194.     }   
  195.   
  196.     private void readObject(ObjectInputStream in)   
  197.         throws IOException, ClassNotFoundException   
  198.     {   
  199.         boolean fromurl = in.readBoolean();   
  200.         if(fromurl)   
  201.         {   
  202.             int offset = in.readInt();   
  203.             String href = (String)in.readObject();   
  204.             in.defaultReadObject();   
  205.             if(null != getUrl())   
  206.             {   
  207.                 URL url = new URL(getUrl());   
  208.                 try  
  209.                 {   
  210.                     setConnection(url.openConnection());   
  211.                 }   
  212.                 catch(ParserException pe)   
  213.                 {   
  214.                     throw new IOException(pe.getMessage());   
  215.                 }   
  216.             }   
  217.             Cursor cursor = new Cursor(this0);   
  218.             for(int i = 0; i < offset; i++)   
  219.                 try  
  220.                 {   
  221.                     getCharacter(cursor);   
  222.                 }   
  223.                 catch(ParserException pe)   
  224.                 {   
  225.                     throw new IOException(pe.getMessage());   
  226.                 }   
  227.   
  228.             setUrl(href);   
  229.         } else  
  230.         {   
  231.             String href = (String)in.readObject();   
  232.             in.defaultReadObject();   
  233.             setUrl(href);   
  234.         }   
  235.     }   
  236.   
  237.     public void reset()   
  238.     {   
  239.         getSource().reset();   
  240.         mIndex = new PageIndex(this);   
  241.     }   
  242.   
  243.     public void close()   
  244.         throws IOException   
  245.     {   
  246.         if(null != getSource())   
  247.             getSource().destroy();   
  248.     }   
  249.   
  250.     protected void finalize()   
  251.         throws Throwable   
  252.     {   
  253.         close();   
  254.     }   
  255.   
  256.     public URLConnection getConnection()   
  257.     {   
  258.         return mConnection;   
  259.     }   
  260.   
  261.     public void setConnection(URLConnection connection)   
  262.         throws ParserException   
  263.     {   
  264.         mConnection = connection;   
  265.         mConnection.setConnectTimeout(6000);   
  266.         mConnection.setReadTimeout(6000);   
  267.         try  
  268.         {   
  269.             getConnection().connect();   
  270.         }   
  271.         catch(UnknownHostException uhe)   
  272.         {   
  273.             throw new ParserException("Connect to " + mConnection.getURL().toExternalForm() + " failed.", uhe);   
  274.         }   
  275.         catch(IOException ioe)   
  276.         {   
  277.             throw new ParserException("Exception connecting to " + mConnection.getURL().toExternalForm() + " (" + ioe.getMessage() + ").", ioe);   
  278.         }   
  279.         String type = getContentType();   
  280.         String charset = getCharset(type);   
  281.         try  
  282.         {   
  283.             String contentEncoding = connection.getContentEncoding();   
  284.             System.out.println("contentEncoding="+contentEncoding);   
  285.             Stream stream;   
  286.             if(null != contentEncoding && -1 != contentEncoding.indexOf("gzip"))   
  287.                 stream = new Stream(new GZIPInputStream(getConnection().getInputStream()));   
  288.             else  
  289.             if(null != contentEncoding && -1 != contentEncoding.indexOf("deflate"))   
  290.                 stream = new Stream(new InflaterInputStream(getConnection().getInputStream(), new Inflater(true)));   
  291.             else{   
  292.                 stream = new Stream(getConnection().getInputStream());   
  293.             }   
  294.   
  295.             try  
  296.             {   
  297.         /*  
  298.                  * 时间:2008年12月23日  
  299.                  * 原因:当String charset = getCharset(type);返回来的是ISO-8859-1的时候,需要处理一下  
  300.                  */  
  301.                 if(charset.indexOf("ISO-8859-1")!=-1){   
  302.                     charset = getQICHAODEFAULT_CHARSET();   
  303.                 }   
  304.          mSource = new InputStreamSource(stream, charset);   
  305.             }   
  306.             catch(UnsupportedEncodingException uee)   
  307.             {   
  308.                 charset = "ISO-8859-1";   
  309.                 mSource = new InputStreamSource(stream, charset);   
  310.             }   
  311.         }   
  312.         catch(IOException ioe)   
  313.         {   
  314.             throw new ParserException("Exception getting input stream from " + mConnection.getURL().toExternalForm() + " (" + ioe.getMessage() + ").", ioe);   
  315.         }   
  316.         mUrl = connection.getURL().toExternalForm();   
  317.         mIndex = new PageIndex(this);   
  318.     }   
  319.   
  320.     public String getUrl()   
  321.     {   
  322.         return mUrl;   
  323.     }   
  324.   
  325.     public void setUrl(String url)   
  326.     {   
  327.         mUrl = url;   
  328.     }   
  329.   
  330.     public String getBaseUrl()   
  331.     {   
  332.         return mBaseUrl;   
  333.     }   
  334.   
  335.     public void setBaseUrl(String url)   
  336.     {   
  337.         mBaseUrl = url;   
  338.     }   
  339.   
  340.     public Source getSource()   
  341.     {   
  342.         return mSource;   
  343.     }   
  344.   
  345.     public String getContentType()   
  346.     {   
  347.         String ret = "text/html";   
  348.         URLConnection connection = getConnection();   
  349.         if(null != connection)   
  350.         {   
  351.             String content = connection.getHeaderField("Content-Type");   
  352.             if(null != content)   
  353.                 ret = content;   
  354.         }   
  355.         return ret;   
  356.     }   
  357.   
  358.     public char getCharacter(Cursor cursor)   
  359.         throws ParserException   
  360.     {   
  361.         int i = cursor.getPosition();   
  362.         int offset = mSource.offset();   
  363.         char ret;   
  364.         if(offset == i)   
  365.             try  
  366.             {   
  367.                 i = mSource.read();   
  368.                 if(-1 == i)   
  369.                 {   
  370.                     ret = '\uFFFF';   
  371.                 } else  
  372.                 {   
  373.                     ret = (char)i;   
  374.                     cursor.advance();   
  375.                 }   
  376.             }   
  377.             catch(IOException ioe)   
  378.             {   
  379.                 throw new ParserException("problem reading a character at position " + cursor.getPosition(), ioe);   
  380.             }   
  381.         else  
  382.         if(offset > i)   
  383.         {   
  384.             try  
  385.             {   
  386.                 ret = mSource.getCharacter(i);   
  387.             }   
  388.             catch(IOException ioe)   
  389.             {   
  390.                 throw new ParserException("can't read a character at position " + i, ioe);   
  391.             }   
  392.             cursor.advance();   
  393.         } else  
  394.         {   
  395.             throw new ParserException("attempt to read future characters from source " + i + " > " + mSource.offset());   
  396.         }   
  397.         if('\r' == ret)   
  398.         {   
  399.             ret = '\n';   
  400.             if(mSource.offset() == cursor.getPosition())   
  401.                 try  
  402.                 {   
  403.                     i = mSource.read();   
  404.                     if(-1 != i)   
  405.                         if('\n' == (char)i)   
  406.                             cursor.advance();   
  407.                         else  
  408.                             try  
  409.                             {   
  410.                                 mSource.unread();   
  411.                             }   
  412.                             catch(IOException ioe)   
  413.                             {   
  414.                                 throw new ParserException("can't unread a character at position " + cursor.getPosition(), ioe);   
  415.                             }   
  416.                 }   
  417.                 catch(IOException ioe)   
  418.                 {   
  419.                     throw new ParserException("problem reading a character at position " + cursor.getPosition(), ioe);   
  420.                 }   
  421.             else  
  422.                 try  
  423.                 {   
  424.                     if('\n' == mSource.getCharacter(cursor.getPosition()))   
  425.                         cursor.advance();   
  426.                 }   
  427.                 catch(IOException ioe)   
  428.                 {   
  429.                     throw new ParserException("can't read a character at position " + cursor.getPosition(), ioe);   
  430.                 }   
  431.         }   
  432.         if('\n' == ret)   
  433.             mIndex.add(cursor);   
  434.         return ret;   
  435.     }   
  436.   
  437.     public void ungetCharacter(Cursor cursor)   
  438.         throws ParserException   
  439.     {   
  440.         cursor.retreat();   
  441.         int i = cursor.getPosition();   
  442.         try  
  443.         {   
  444.             char ch = mSource.getCharacter(i);   
  445.             if('\n' == ch && 0 != i)   
  446.             {   
  447.                 ch = mSource.getCharacter(i - 1);   
  448.                 if('\r' == ch)   
  449.                     cursor.retreat();   
  450.             }   
  451.         }   
  452.         catch(IOException ioe)   
  453.         {   
  454.             throw new ParserException("can't read a character at position " + cursor.getPosition(), ioe);   
  455.         }   
  456.     }   
  457.   
  458.     public String getEncoding()   
  459.     {   
  460.         return getSource().getEncoding();   
  461.     }   
  462.   
  463.     public void setEncoding(String character_set)   
  464.         throws ParserException   
  465.     {   
  466.         this.QICHAODEFAULT_CHARSET = character_set;   
  467.         getSource().setEncoding(character_set);   
  468.     }   
  469.   
  470.     public URL constructUrl(String link, String base)   
  471.         throws MalformedURLException   
  472.     {   
  473.         return constructUrl(link, base, false);   
  474.     }   
  475.   
  476.     public URL constructUrl(String link, String base, boolean strict)   
  477.         throws MalformedURLException   
  478.     {   
  479.         int index;   
  480.         URL url;   
  481.         if(!strict && '?' == link.charAt(0))   
  482.         {   
  483.             if(-1 != (index = base.lastIndexOf('?')))   
  484.                 base = base.substring(0, index);   
  485.             url = new URL(base + link);   
  486.         } else  
  487.         {   
  488.             url = new URL(new URL(base), link);   
  489.         }   
  490.         String path = url.getFile();   
  491.         boolean modified = false;   
  492.         boolean absolute = link.startsWith("/");   
  493.         if(!absolute)   
  494.             do  
  495.             {   
  496.                 if(!path.startsWith("/."))   
  497.                     break;   
  498.                 if(path.startsWith("/../"))   
  499.                 {   
  500.                     path = path.substring(3);   
  501.                     modified = true;   
  502.                     continue;   
  503.                 }   
  504.                 if(!path.startsWith("/./") && !path.startsWith("/."))   
  505.                     break;   
  506.                 path = path.substring(2);   
  507.                 modified = true;   
  508.             } while(true);   
  509.         while(-1 != (index = path.indexOf("/\\")))    
  510.         {   
  511.             path = path.substring(0, index + 1) + path.substring(index + 2);   
  512.             modified = true;   
  513.         }   
  514.         if(modified)   
  515.             url = new URL(url, path);   
  516.         return url;   
  517.     }   
  518.   
  519.     public String getAbsoluteURL(String link)   
  520.     {   
  521.         return getAbsoluteURL(link, false);   
  522.     }   
  523.   
  524.     public String getAbsoluteURL(String link, boolean strict)   
  525.     {   
  526.         String ret;   
  527.         if(null == link || "".equals(link))   
  528.             ret = "";   
  529.         else  
  530.             try  
  531.             {   
  532.                 String base = getBaseUrl();   
  533.                 if(null == base)   
  534.                     base = getUrl();   
  535.                 if(null == base)   
  536.                 {   
  537.                     ret = link;   
  538.                 } else  
  539.                 {   
  540.                     URL url = constructUrl(link, base, strict);   
  541.                     ret = url.toExternalForm();   
  542.                 }   
  543.             }   
  544.             catch(MalformedURLException murle)   
  545.             {   
  546.                 ret = link;   
  547.             }   
  548.         return ret;   
  549.     }   
  550.   
  551.     public int row(Cursor cursor)   
  552.     {   
  553.         return mIndex.row(cursor);   
  554.     }   
  555.   
  556.     public int row(int position)   
  557.     {   
  558.         return mIndex.row(position);   
  559.     }   
  560.   
  561.     public int column(Cursor cursor)   
  562.     {   
  563.         return mIndex.column(cursor);   
  564.     }   
  565.   
  566.     public int column(int position)   
  567.     {   
  568.         return mIndex.column(position);   
  569.     }   
  570.   
  571.     public String getText(int start, int end)   
  572.         throws IllegalArgumentException   
  573.     {   
  574.         String ret;   
  575.         try  
  576.         {   
  577.             ret = mSource.getString(start, end - start);   
  578.         }   
  579.         catch(IOException ioe)   
  580.         {   
  581.             throw new IllegalArgumentException("can't get the " + (end - start) + "characters at position " + start + " - " + ioe.getMessage());   
  582.         }   
  583.         return ret;   
  584.     }   
  585.   
  586.     public void getText(StringBuffer buffer, int start, int end)   
  587.         throws IllegalArgumentException   
  588.     {   
  589.         if(mSource.offset() < start || mSource.offset() < end)   
  590.             throw new IllegalArgumentException("attempt to extract future characters from source" + start + "|" + end + " > " + mSource.offset());   
  591.         int length;   
  592.         if(end < start)   
  593.         {   
  594.             length = end;   
  595.             end = start;   
  596.             start = length;   
  597.         }   
  598.         length = end - start;   
  599.         try  
  600.         {   
  601.             mSource.getCharacters(buffer, start, length);   
  602.         }   
  603.         catch(IOException ioe)   
  604.         {   
  605.             throw new IllegalArgumentException("can't get the " + (end - start) + "characters at position " + start + " - " + ioe.getMessage());   
  606.         }   
  607.     }   
  608.   
  609.     public String getText()   
  610.     {   
  611.         return getText(0, mSource.offset());   
  612.     }   
  613.   
  614.     public void getText(StringBuffer buffer)   
  615.     {   
  616.         getText(buffer, 0, mSource.offset());   
  617.     }   
  618.   
  619.     public void getText(char array[], int offset, int start, int end)   
  620.         throws IllegalArgumentException   
  621.     {   
  622.         if(mSource.offset() < start || mSource.offset() < end)   
  623.             throw new IllegalArgumentException("attempt to extract future characters from source");   
  624.         int length;   
  625.         if(end < start)   
  626.         {   
  627.             length = end;   
  628.             end = start;   
  629.             start = length;   
  630.         }   
  631.         length = end - start;   
  632.         try  
  633.         {   
  634.             mSource.getCharacters(array, offset, start, end);   
  635.         }   
  636.         catch(IOException ioe)   
  637.         {   
  638.             throw new IllegalArgumentException("can't get the " + (end - start) + "characters at position " + start + " - " + ioe.getMessage());   
  639.         }   
  640.     }   
  641.   
  642.     public String getLine(Cursor cursor)   
  643.     {   
  644.         int line = row(cursor);   
  645.         int size = mIndex.size();   
  646.         int start;   
  647.         int end;   
  648.         if(line < size)   
  649.         {   
  650.             start = mIndex.elementAt(line);   
  651.             if(++line <= size)   
  652.                 end = mIndex.elementAt(line);   
  653.             else  
  654.                 end = mSource.offset();   
  655.         } else  
  656.         {   
  657.             start = mIndex.elementAt(line - 1);   
  658.             end = mSource.offset();   
  659.         }   
  660.         return getText(start, end);   
  661.     }   
  662.   
  663.     public String getLine(int position)   
  664.     {   
  665.         return getLine(new Cursor(this, position));   
  666.     }   
  667.   
  668.     public String toString()   
  669.     {   
  670.         String ret;   
  671.         if(mSource.offset() > 0)   
  672.         {   
  673.             StringBuffer buffer = new StringBuffer(43);   
  674.             int start = mSource.offset() - 40;   
  675.             if(0 > start)   
  676.                 start = 0;   
  677.             else  
  678.                 buffer.append("...");   
  679.             getText(buffer, start, mSource.offset());   
  680.             ret = buffer.toString();   
  681.         } else  
  682.         {   
  683.             ret = super.toString();   
  684.         }   
  685.         return ret;   
  686.     }   
  687.   
  688.     public static final String DEFAULT_CHARSET = "ISO-8859-1";   
  689.     public static String QICHAODEFAULT_CHARSET = "gb2312";   
  690.     public static final String DEFAULT_CONTENT_TYPE = "text/html";   
  691.     public static final char EOF = 65535;   
  692.     protected String mUrl;   
  693.     protected String mBaseUrl;   
  694.     protected Source mSource;   
  695.     protected PageIndex mIndex;   
  696.     protected transient URLConnection mConnection;   
  697.     protected static ConnectionManager mConnectionManager = new ConnectionManager();   
  698.     public static String getQICHAODEFAULT_CHARSET() {   
  699.         return QICHAODEFAULT_CHARSET;   
  700.     }   
  701.   
  702. }  
 

在调用的时候,代码如下:

Java代码
 
  1. Parser parser = new Parser(url);   
  2. parser.setEncoding(parser.getLexer().getPage().getQICHAODEFAULT_CHARSET());  

  一般情况下,设置成这样应该是没问题的啦,但是,你有时候看到的编码方式并不一定是它该网页的编码方式。比如说,肉眼看到页面中有<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />,htmlparser可以正常的获取到,代码如下:

Java代码
 
  1. public void doSemanticAction()   
  2.     throws ParserException   
  3. {   
  4.     String httpEquiv = getHttpEquiv();   
  5.     if("Content-Type".equalsIgnoreCase(httpEquiv))   
  6.     {   
  7.         String charset = getPage().getCharset(getAttribute("CONTENT"));   
  8.         getPage().setEncoding(charset);   
  9.     }   
  10. }  

 但是,你不要认为这个就是它的编码方式啦,在htmlparser,还进行了一次判断,在类Page中,有个方法是获取报头字段Content-Type的。代码如下:

Java代码
 
  1. public String getContentType()   
  2. {   
  3.     String ret = "text/html";   
  4.     URLConnection connection = getConnection();   
  5.     if(null != connection)   
  6.     {   
  7.         String content = connection.getHeaderField("Content-Type");   
  8.         if(null != content)   
  9.             ret = content;   
  10.     }   
  11.     return ret;   
  12. }  

 两个进行比较,如果不一样的话,它就报了

Java代码
 
  1. org.htmlparser.util.EncodingChangeException: character mismatch (new: 中 [0x4e2d] != old:  [0xd6?]) for encoding change from ISO-8859-1 to GB2312 at character offset 23  

 ,所以,我在类InputStreamSource再一次进行修改:

代码如下:

Java代码
 
  1. public void setEncoding(String character_set)   
  2.     throws ParserException   
  3. {   
  4.     String encoding = getEncoding();   
  5.     /**  
  6.      * time:2008年12月23日  
  7.      */  
  8.     if(encoding!=null){   
  9.         character_set = encoding;   
  10.     }   
  11.     if(!encoding.equalsIgnoreCase(character_set))   
  12.     {   
  13.         InputStream stream = getStream();   
  14.         try  
  15.         {   
  16.             char buffer[] = mBuffer;   
  17.             int offset = mOffset;   
  18.             stream.reset();   
  19.             try  
  20.             {   
  21.                 mEncoding = character_set;   
  22.                 mReader = new InputStreamReader(stream, character_set);   
  23.                 mBuffer = new char[mBuffer.length];   
  24.                 mLevel = 0;   
  25.                 mOffset = 0;   
  26.                 mMark = -1;   
  27.                 if(0 != offset)   
  28.                 {   
  29.                     char new_chars[] = new char[offset];   
  30.                     if(offset != read(new_chars))   
  31.                         throw new ParserException("reset stream failed");   
  32.                     for(int i = 0; i < offset; i++)   
  33.                         if(new_chars[i] != buffer[i])   
  34.                             throw new EncodingChangeException("character mismatch (new: " + new_chars[i] + " [0x" + Integer.toString(new_chars[i], 16) + "] != old: " + " [0x" + Integer.toString(buffer[i], 16) + buffer[i] + "]) for encoding change from " + encoding + " to " + character_set + " at character offset " + i);   
  35.   
  36.                 }   
  37.             }   
  38.             catch(IOException ioe)   
  39.             {   
  40.                 throw new ParserException(ioe.getMessage(), ioe);   
  41.             }   
  42.         }   
  43.         catch(IOException ioe)   
  44.         {   
  45.             throw new ParserException("Stream reset failed (" + ioe.getMessage() + "), try wrapping it with a org.htmlparser.lexer.Stream", ioe);   
  46.         }   
  47.     }   
  48. }  

 这样应该来说,不管什么方式都OK的。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C#算法实现字符串反转
mysql自动生成大量数据
LeetCode之Reverse String II
jbpm的type问题,big bug!
Service .java
让我的C++程序直接阅读网页(4) HTTP访问
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服