Java访问Url地址并下载文件

package com.yangjuqi.resource;

import java.io.*;

import java.net.URL;

/** * java访问URL并下载文件 * @author yangjuqi 2007-12-14 下午04:08:51 * */

public class ResourceURLServer

{

  public static void getURLResource(String ourputFile,String urlStr) throws Exception

{   

      FileWriter fw = new FileWriter(ourputFile);   

      PrintWriter pw = new PrintWriter(fw);   

      URL resourceUrl = new URL(urlStr);   

      InputStream content = (InputStream) resourceUrl.getContent();   

      BufferedReader in = new BufferedReader(new InputStreamReader(content));   

      String line;   

      while ((line = in.readLine()) != null)

    {    

        pw.println(line);   

    }   

       pw.close();   

       fw.close();

}

public static void main(String[] args)

{   

      try

     {    

            System.out.println("begin");    

            getURLResource("f:\\测试.xls","http://localhost:8088/bgcl/bussi/ticketQuery.jsp?excel=excel");    

            System.out.println("end");   

}

       catch (Exception e)

     {    

           e.printStackTrace();   

     }

}

}

posted on 2012-10-13 14:39  专注2018  阅读(9371)  评论(0编辑  收藏  举报