2

JavaでURLを開くには?

E.g i have https://www.iformbuilder.com/exzact/dataExcelFeed.php?
PAGE_ID=3175952&TABLE_NAME=_data399173_eff_sor&ANALYTIC=1&SINCE_DATE=2013-10-1

この URL にアクセスすると、ファイルがダウンロードされます。しかし、これをコードに実装するにはどうすればよいでしょうか。

ファイルがダウンロードされるようにURLを開くためにこれを試みますが、機能しません。

URL url = new URL("https://www.iformbuilder.com/exzact/dataExcelFeed.php?PAGE_ID=3175952&TABLE_NAME=_data399173_eff_sor&ANALYTIC=1&SINCE_DATE=2013-10-16");
    HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
    System.out.println(urlCon);
    urlCon.connect();

私は何かが間違っていることを知っている

4

3 に答える 3

2

私はこれを見つけました:

URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

この記事の内容: Java を使用してインターネットからファイルをダウンロードして保存する方法

これがお役に立てば幸いです

于 2013-10-16T05:43:41.940 に答える
1

これを試して:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

class HttpHelperx
{
   public static String GET(String url)
   {
      String result = "";

      try 
      {
         URL navUrl = new URL(url);
         URLConnection con = (URLConnection)navUrl.openConnection();

         result = getContent(con);

      } 
      catch (MalformedURLException e) 
      {
         e.printStackTrace();
      } 
      catch (IOException e) 
      {
         e.printStackTrace();
      }

      return result;
   }

   public static String getContent(URLConnection con)
   {
      String result = "";
      if(con!=null)
      {
         BufferedReader br;
         try 
         {
            br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuilder buffer = new StringBuilder();
            String aux = "";

            while ((aux = br.readLine()) != null)
            {
               buffer.append(aux);
            }
            result = buffer.toString();
            br.close();
         } 
         catch (IOException e) 
         {
            e.printStackTrace();
         }
      }

      return result;
   }
}

使用するには:

public class HTTPHelperDriver
{
   public static void main(String[] args)
      throws Exception
   {
      String response = HttpHelperx.GET("http://www.google.com");
      System.out.println(response); 
   }
}
于 2013-10-16T05:43:19.770 に答える