通过下载对话框从URL下载Java文件 [英] Java Download file from url with download dialog

查看:76
本文介绍了通过下载对话框从URL下载Java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从最初显示一些html的url下载文件,然后在2-3秒后显示一个下载对话框。显然,如果我这样做:

I want to download a file from a url which initially shows some html, then displays a download dialog after 2-3 seconds. Obviously if I do this:

     try {
            URL url = new URL("http://my.url");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("Content-Length",
                    Integer.toString(Integer.MAX_VALUE));
            con.setReadTimeout(Integer.MAX_VALUE);
            con.setConnectTimeout(Integer.MAX_VALUE);
            con.connect();

            bis = new BufferedInputStream(con.getInputStream(), 4096);

            byteArray = IOUtils.toByteArray(bis);

            FileUtils.writeByteArrayToFile(new File("myFile"), byteArray);


        } catch (Exception e) {
        }

我将保存显示的.html而不是保存对话框中显示的文件。
我应该如何更改代码才能做到这一点?

I will save the displayed .html rather than the file that is displayed in the save dialog. How should I change the code in order to do this?

推荐答案

我猜测对话框中只有一些内容javascript,它会等待几秒钟,并请求将文件下载嵌入对话框中某个位置。

I'm guessing the dialog just has some javascript which waits a couple seconds and requests the file download embedded in the dialog somewhere.

如果是这种情况,如果您确定实际下载是什么元素包含在其中,您可以使用JSoup或任何其他html解析器库将链接从页面中抓取。

If this is the case, if you figure out what element the 'real download' is contained in, you can use JSoup, or any other html parser library to scrape the link out of the page.

显然,只有下载链接为

完成rossa建议的操作后,我建议在对话框窗口中设置javascript断点,以弄清楚实际请求的URL的精确度。

After doing what rossa suggests, I'd suggest setting javascript breakpoints in the dialog window to figure out how exactly the real url is getting requested.

这篇关于通过下载对话框从URL下载Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆