简体   繁体   中英

Not able to download file from internet url in java

i want to download a csv file from a internet url. 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 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. If I enter myurl in a browser i get a popup to save or download the csv file. So the problem is not with an incorrrect "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.

Thanks everyone . Used httpclient and it works for me. Heres the code that downloads an csv file , saves it locally and then reads it and parses all the tokens.

        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? I used it sometime ago to download a set of exams from a site automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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