无法从java下载文件

时间:2017-04-24 07:06:27

标签: java

我写了一个程序来从雅虎财经网站获取股票数据,我的代码以前曾经工作过,最近它已经停止工作了。 当我从浏览器访问相同的URL时,会下载一个文件, 但是从java代码我得到和空流

这里是sample link

这些是我尝试的代码

try{


        ReadableByteChannel rbc = Channels
                .newChannel(website.openStream());
        FileOutputStream fos;
        fos = new FileOutputStream(Type+"//"+
                FileName + ".csv");
        fos.getChannel().transferFrom(rbc, 0,
                Long.MAX_VALUE);
        fos.flush();
        fos.close();


        String fileName = "file.txt"; //The file that will be saved on your computer                

         URL link = new URL(website.toString());
         //Code to download
         InputStream in = new BufferedInputStream(link.openStream());
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         byte[] buf = new byte[1024];
         int n = 0;
         while (-1!=(n=in.read(buf)))
         {
            out.write(buf, 0, n);
         }
         out.close();
         in.close();
         byte[] response = out.toByteArray();

         FileOutputStream fos = new FileOutputStream(fileName);
         fos.write(response);
         fos.close();
     //End download code

        Runnable r1 = new Analyzer(Type+"//"+
                FileName + ".csv",Type,Name);
    Thread r2=  new Thread(r1);



r2.start();
r2.join();



}
catch(Exception e)
{
e.getMessage();

}

0 个答案:

没有答案