Java URL问题
网页包含可执行文件的链接(即,如果我们单击该链接,浏览器会将文件下载到您的本地计算机上)。
有没有办法用Java实现同样的功能?
谢谢
A webpage contains a link to an executable (i.e. If we click on the link, the browser will download the file on your local machine).
Is there any way to achieve the same functionality with Java?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,有。
这里有一个简单的例子:
您可以有一个 JSF(Java Server Faces)页面,带有一个支持支持 bean,其中包含一个用
@PostConstruct
注释的方法,这意味着任何操作(例如下载)都会发生创建页面时。已经有一个非常相似的问题,请查看: Invoke JSF Managed bean Action on page load
Yes there is.
Here a simple example:
You can have a JSF(Java Server Faces) page, with a supporting backing bean that contains a method annotated with
@PostConstruct
This means that any action(for example downloading), will occur when the page is created.There is already a question very similar already, have a look at: Invoke JSF managed bean action on page load
可以使用Java的URL类来下载一个文件,但需要做一些工作。您需要执行以下操作:
openStream()
以获取InputStream
FileOutputStream
)InputStream
并写入文件,直到没有更多数据可供读取您正在下载的文件的类型并不重要(事实上它是一个可执行文件是无关紧要的),因为对于任何类型的文件,该过程都是相同的。
更新:听起来您真正想要的是将网页的URL插入Java应用程序,并让Java应用程序找到页面中的链接,然后下载该链接。如果是这种情况,您的问题的措辞非常不清楚,但以下是我将使用的基本步骤:
InputStream
元素并提取其
href
属性以获取该 URL您需要下载的文件(如果它是相对 URL 而不是绝对 URL,则需要根据原始页面的 URL 解析该 URL)以下是基于
jsoup< 的一个小快捷方式/code> (我已经以前从未使用过,我只是从他们的网页上窃取的片段中编写此内容)。我省略了很多错误检查,但是,嘿,我通常为此收费:
确定目标文件名应该是什么的正确方法实际上是寻找
Content-Disposition
标头,并查找filename=
之后的位。在这种情况下,您无法在 URL 上使用openStream()
,而是需要使用openConnection()
来获取URLConnection
。然后,您可以使用getInputStream()
获取您的InputStream
和getRequestProperty("Content-Disposition")
获取标头以找出您的文件名。如果标头丢失或格式错误,您应该转而使用上述方法来确定目标文件名。You can use Java's, URL class to download a file, but it requires a little work. You will need to do the following:
openStream()
to get anInputStream
FileOutputStream
)InputStream
and write to the file, until there is no more data left to readIt doesn't really matter what type of file you are downloading (the fact that it's an executable file is irrelevant) since the process is the same for any type of file.
Update: It sounds like what you actually want is to plug the URL of a webpage into the Java app, and have the Java app find the link in the page and then download that link. If that is the case, the wording of your question is very unclear, but here are the basic steps I would use:
InputStream
for the page<a>
element that you want and extract itshref
attribute to get the URL of the file you need to download (if it's a relative URL instead of absolute, you will need to resolve that URL against the URL of the original page)Here's a slight shortcut, based on
jsoup
(which I've never used before, I'm just writing this from snippets stolen from their webpage). I've left out a lot of error checking, but hey, I usually charge for this:The proper way to determine what the destination filename should be (unless you hardcode it) is actually to look for the
Content-Disposition
header, and look for the bit afterfilename=
. In that case, you can't useopenStream()
on the URL, you will need to useopenConnection()
instead, to get aURLConnection
. Then you can usegetInputStream()
to get yourInputStream
andgetRequestProperty("Content-Disposition")
to get the header to figure out your filename. In case that header is missing or malformed, you should then fall-back to using the method above to determine the destination filename.您可以使用 apache commons IO FileUtils
http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL, java.io.File)
编辑:
我能够从源锻造站点成功下载 zip 文件(它不是空的),它做了类似的事情
我也能够成功下载 tomcat.exe
URL url = 新 URL("http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.exe");
You can do this using apache commons IO FileUtils
http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL, java.io.File)
Edit:
I was able to successfully download a zip file from source forge site (it is not empty), It did some thing like this
I was able successfully download tomcat.exe too
URL url = new URL("http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.exe");