简体   繁体   English

无法从Java中的Internet URL下载文件

[英]Not able to download file from internet url in java

i want to download a csv file from a internet url. 我想从互联网网址下载csv文件。 I have tried every code that i can find online and not able to download. 我尝试了所有可以在网上找到但无法下载的代码。

           URL google = new URL("myurl");
           ReadableByteChannel rbc = Channels.newChannel(google.openStream());
           FileOutputStream fos = new FileOutputStream("C://excelsheet.csv");
           fos.getChannel().transferFrom(rbc, 0, 1 << 24);

This code is not working 该代码不起作用

I used this function , it is not working too. 我使用了此功能,它也不起作用。

public stacvoid saveUrl(String filename, String urlString) throws MalformedURLException, IOException
    {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try
        {
                in = new BufferedInputStream(new URL(urlString).openStream());
                fout = new FileOutputStream(filename);

                byte data[] = new byte[1024];
                int count;
                while ((count = in.read(data, 0, 1024)) != -1)
                {
                        fout.write(data, 0, count);
                }
        }
        finally
        {
                if (in != null)
                        in.close();
                if (fout != null)
                        fout.close();
        }
    }

I tried a simple input stream on my url , that doesn't work too. 我在url上尝试了一个简单的输入流,但也无法正常工作。

           URL oracle = new URL("myurl");
           URLConnection yc = oracle.openConnection();

      BufferedReader br = new BufferedReader(new InputStreamReader(
                                          yc.getInputStream()));

Now instead of myurl , if i type any other url , the bufferedreader does get data. 现在代替myurl,如果我键入任何其他url,则bufferedreader确实会获取数据。 If I enter myurl in a browser i get a popup to save or download the csv file. 如果在浏览器中输入myurl,我会弹出一个对话框来保存或下载csv文件。 So the problem is not with an incorrrect "myurl" 因此,问题不在于错误的“ myurl”

Is it possible that the servelet/code running on "myurl" actually checks if the request is coming from a browser and only then sends the data. 运行在“ myurl”上的servelet /代码是否可能实际上检查请求是否来自浏览器,然后才发送数据。

Thanks everyone . 感谢大家 。 Used httpclient and it works for me. 使用了httpclient,对我有用。 Heres the code that downloads an csv file , saves it locally and then reads it and parses all the tokens. 这里是下载csv文件,将其保存在本地然后读取并解析所有标记的代码。

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("your url to download");
        HttpResponse response = httpclient.execute(httpget);

           System.out.println(response.getProtocolVersion());
           System.out.println(response.getStatusLine().getStatusCode());
           System.out.println(response.getStatusLine().getReasonPhrase());
           System.out.println(response.getStatusLine().toString());
            HttpEntity entity =    response.getEntity();
           if (entity != null) {
               FileOutputStream fos = new java.io.FileOutputStream("C://excelsheet.csv");
               entity.writeTo(fos);
               fos.close();
           }

           //create BufferedReader to read csv file
      BufferedReader br = new BufferedReader( new FileReader("C://excelsheet.csv"));
      String strLine = "";
      StringTokenizer st = null;
       int lineNumber = 0, tokenNumber = 0;
      while( (strLine = br.readLine()) != null)
                              {
                                      lineNumber++;

                                      //break comma separated line using ","
                                      st = new StringTokenizer(strLine, ",");

                                      while(st.hasMoreTokens())
                                      {
                                              //display csv values
                                              tokenNumber++;
                                              System.out.println("Line # " + lineNumber +
                                                              ", Token # " + tokenNumber
                                                              + ", Token : "+ st.nextToken());
                                      }

                                      //reset token number
                                      tokenNumber = 0;

                              } 
       }
       catch (Exception e) {
           System.out.println("insdie the catch part");
           System.out.println(e.toString());
       }

Have you tried the HttpClient lib? 您是否尝试过HttpClient库? I used it sometime ago to download a set of exams from a site automatically. 我前段时间使用它从网站上自动下载了一组考试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM