Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中打开URL_Java_Download - Fatal编程技术网

在java中打开URL

在java中打开URL,java,download,Java,Download,如何在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("htt

如何在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();
我知道有点不对劲,但我不知道该怎么办

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); 
   }
}
我发现:

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打开和读取URL,谷歌搜索会有所帮助。这里是在查询“URL java read”时返回的第一个链接,该链接可能与