当前位置: 代码迷 >> Android >> java抓取网页,打印时乱码,该如何处理
  详细解决方案

java抓取网页,打印时乱码,该如何处理

热度:23   发布时间:2016-05-01 10:07:30.0
java抓取网页,打印时乱码
下面的方法是把网页保存到SD卡一个文件中,打开时没有乱码,打印时却有乱码。
//将页面保存到本地文件夹
    public void doWrite(String url_str)
    {
     URL url = null;

     FileOutputStream fos = null;

     InputStream is;

     try {

     for (int i = 0; i < 1; i++) {

     url = new URL(url_str);
     ByteArrayOutputStream data=new ByteArrayOutputStream();

     byte bytes[] = new byte[1024 * 2000];

     int index = 0;

     is = url.openStream();

     int count = is.read(bytes, index, 1024 * 2000);

     while (count != -1) {

     index += count;

     count = is.read(bytes, index, 1);

     }
    
     url1=System.currentTimeMillis();//保存名设置

     fos = new FileOutputStream("sdcard/dyhtml/"+url1+".html");
     fos.write(bytes, 0, index);
    
     is.close();
     fos.close();
     }
     }catch (MalformedURLException e) {

     e.printStackTrace();

     } catch (IOException e) {

     e.printStackTrace();

     }
  
    }
Java 乱码

------解决方案--------------------
java 默认utf-8,试试抓一个utf8的网页看看
------解决方案--------------------
可以指定编码
new FileOutputStream("out.txt"),"GB2312")

------解决方案--------------------