Posted By

multani on 10/27/08


Tagged

java page download web


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

bsteadman
appx
vkolev


Java download the web page


 / Published in: Java
 

URL: http://www.zhuoda.org/lunzi/90049.html

  1. import java.io.BufferedReader;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.io.PrintWriter;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10.  
  11. public class Test {
  12.  
  13. /**
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. try {
  18. URL url = new URL("http://jj.24365pt.com/index.jhtml");
  19.  
  20. URLConnection conn = url.openConnection();
  21. conn.setDoOutput(true);
  22. InputStream in = null;
  23. in = url.openStream();
  24. String content = pipe(in,"utf-8");
  25. System.out.println(content);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. static String pipe(InputStream in,String charset) throws IOException {
  32. if(charset==null||"".equals(charset)){
  33. charset="utf-8";
  34. }
  35. String rLine = null;
  36. BufferedReader bReader = new BufferedReader(new InputStreamReader(in,charset));
  37. PrintWriter pw = null;
  38.  
  39. FileOutputStream fo = new FileOutputStream("../index.html");
  40. OutputStreamWriter writer = new OutputStreamWriter(fo, "utf-8");
  41. pw = new PrintWriter(writer);
  42. while ( (rLine = bReader.readLine()) != null) {
  43. String tmp_rLine = rLine;
  44. int str_len = tmp_rLine.length();
  45. if (str_len > 0) {
  46. s.append(tmp_rLine);
  47. pw.println(tmp_rLine);
  48. pw.flush();
  49. }
  50. tmp_rLine = null;
  51. }
  52. in.close();
  53. pw.close();
  54. return s.toString();
  55. }
  56. }

Report this snippet  

You need to login to post a comment.