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 从 Internet 下载和保存文件?

我希望这可以帮助你

于 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 回答
0

您可以使用以下代码:

try {
    //specify the protocol along with the URL
    URI oURL = new URI("https://www.google.com/");
    desktop.browse(oURL);
} catch (URISyntaxException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
于 2022-01-27T16:29:38.570 回答