当读.jsp的网页时便会出现乱码,请各位高手帮帮忙。小弟在此谢过了。import java.net.*;
import java.io.*;public class URLResource
{
public static void main(String[] args)
{
                           try{      int n= -1;
                                      byte b[]=new byte[118];
                                      
                                      String s1=new String("http://222.180.184.22/PlanAndCurriculum/cour_tab_sel_stud.ASP");
                                      URL url=new  URL(s1);
                                      InputStream in=url.openStream();
                                      RandomAccessFile in_and_out=new   RandomAccessFile(new File("schedule.txt"),"rw");
                                      while((n=in.read(b))!= -1)
                                      {       
                                              in_and_out.write(b,0,n);
                                      }                        
                                      in_and_out.close();
                                      
                               }
                               catch(MalformedURLException e){System.out.println("don't  find this file"+e);}
                               catch(IOException el){System.out.println("io error"+el);}                            
}
}

解决方案 »

  1.   

    Java读写文件最常用的类是FileInputStream/FileOutputStream和FileReader/FileWriter。其中FileInputStream和FileOutputStream是基于字节流的,常用于读写二进制文件。读写字符文件建议使用基于字符的FileReader和FileWriter,省去了字节与字符之间的转换。但这两个类的构造函数默认使用系统的编码方式,如果文件内容与系统编码方式不一致,可能会出现乱码。在这种情况下,建议使用FileReader和FileWriter的父类:InputStreamReader/OutputStreamWriter,它们也是基于字符的,但在构造函数中可以指定编码类型:InputStreamReader(InputStream in, Charset cs) 和OutputStreamWriter(OutputStream out, Charset cs)。 
      

  2.   

    本帖最后由 java2000_net 于 2008-04-12 07:31:16 编辑
      

  3.   

    顶竹大!,你应该用省去了字节与字符之间的转换,把byte类型转换一下。
      

  4.   

    import java.net.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.io.*;
    public class UrlCodeRegex_2
    {
      public static void main(String[] args)
       {
        try 
        {
            String ur="http://222.180.184.22/PlanAndCurriculum/cour_tab_sel_stud.ASP"; 
            URL MyURL=new URL(ur);
            String str;
            URLConnection con=MyURL.openConnection();
            InputStreamReader ins=new InputStreamReader(con.getInputStream(),"gbk");
            BufferedReader in=new  BufferedReader(ins);
            StringBuffer sb = new StringBuffer();
            while ((str=in.readLine())!=null)
            {  
                sb.append(str);
            }
                in.close();
                System.out.println(sb.toString());
         } 
        catch (IOException ioe) {
          System.out.println("IOException: " + ioe);
          
        }
      }
    }编码无非就是gbk,gb2312,utf-8,不行的话,你在我的代码里换换编码就是了
      

  5.   


    根据http协议,连接在不指定keep-alive时,是自动关闭的。呵呵!