Java URL问题

发布于 2024-11-28 03:15:16 字数 90 浏览 1 评论 0原文

网页包含可执行文件的链接(即,如果我们单击该链接,浏览器会将文件下载到您的本地计算机上)。

有没有办法用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×眷恋的温暖 2024-12-05 03:15:16

是的,有。
这里有一个简单的例子:

您可以有一个 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

献世佛 2024-12-05 03:15:16

可以使用Java的URL类来下载一个文件,但需要做一些工作。您需要执行以下操作:

  1. 在文件处创建 URL 对象点
  2. 调用 openStream() 以获取 InputStream
  3. 打开要写入的文件(a FileOutputStream)
  4. 读取InputStream 并写入文件,直到没有更多数据可供读取
  5. 关闭输入和输出流

您正在下载的文件的类型并不重要(事实上它是一个可执行文件是无关紧要的),因为对于任何类型的文件,该过程都是相同的。

更新:听起来您真正想要的是将网页的URL插入Java应用程序,并让Java应用程序找到页面中的链接,然后下载该链接。如果是这种情况,您的问题的措辞非常不清楚,但以下是我将使用的基本步骤:

  1. 首先,使用上面的步骤 1 和 2 获取页面的 InputStream
  2. 使用类似的内容TagSoupjsoup 解析 HTML
  3. 找到所需的 元素并提取其 href 属性以获取该 URL您需要下载的文件(如果它是相对 URL 而不是绝对 URL,则需要根据原始页面的 URL 解析该 URL)
  4. 使用上述步骤下载该 URL

以下是基于 jsoup< 的一个小快捷方式/code> (我已经以前从未使用过,我只是从他们的网页上窃取的片段中编写此内容)。我省略了很多错误检查,但是,嘿,我通常为此收费:

Document doc = Jsoup.connect(pageUrl).get();
Element aElement = doc.getElementsByTag("a").first() // Obviously you may need to refine this
String newUrl = aElement.attr("abs:href"); // This is a piece of jsoup magic that ensures that the destination URL is absolute
// assert newUrl != null
URL fileUrl = new URL(newUrl);
String destPath = fileUrl.getPath();
int lastSlash = destPath.lastIndexOf('/');
if (lastSlash != -1) {
    destPath = destPath.substring(lastSlash);
}
// Assert that this is really a valid filename
// Now just download fileUrl and save it to destPath

确定目标文件名应该是什么的正确方法实际上是寻找Content-Disposition 标头,并查找 filename= 之后的位。在这种情况下,您无法在 URL 上使用 openStream(),而是需要使用 openConnection() 来获取 URLConnection。然后,您可以使用 getInputStream() 获取您的 InputStreamgetRequestProperty("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:

  1. Create the URL object point at the file
  2. Call openStream() to get an InputStream
  3. Open the file you want to write to (a FileOutputStream)
  4. Read from the InputStream and write to the file, until there is no more data left to read
  5. Close the input and output streams

It 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:

  1. First, use steps 1 and 2 above to get an InputStream for the page
  2. Use something like TagSoup or jsoup to parse the HTML
  3. Find the <a> element that you want and extract its href 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)
  4. Use the steps above to download that URL

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:

Document doc = Jsoup.connect(pageUrl).get();
Element aElement = doc.getElementsByTag("a").first() // Obviously you may need to refine this
String newUrl = aElement.attr("abs:href"); // This is a piece of jsoup magic that ensures that the destination URL is absolute
// assert newUrl != null
URL fileUrl = new URL(newUrl);
String destPath = fileUrl.getPath();
int lastSlash = destPath.lastIndexOf('/');
if (lastSlash != -1) {
    destPath = destPath.substring(lastSlash);
}
// Assert that this is really a valid filename
// Now just download fileUrl and save it to destPath

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 after filename=. In that case, you can't use openStream() on the URL, you will need to use openConnection() instead, to get a URLConnection. Then you can use getInputStream() to get your InputStream and getRequestProperty("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.

怪我鬧 2024-12-05 03:15:16

您可以使用 apache commons IO FileUtils

http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL, java.io.File)

编辑:

我能够从源锻造站点成功下载 zip 文件(它不是空的),它做了类似的事情

import java.io.File;
import java.net.URL;

import org.apache.commons.io.FileUtils; 
public class Test 
{

 public static void main(String args[])
 {

    try {
        URL url = new URL("http://sourceforge.net/projects/gallery/files/gallery3/3.0.2/gallery-3.0.2.zip/download");
        FileUtils.copyURLToFile(url, new File("test.zip"));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   }
 }

我也能够成功下载 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

import java.io.File;
import java.net.URL;

import org.apache.commons.io.FileUtils; 
public class Test 
{

 public static void main(String args[])
 {

    try {
        URL url = new URL("http://sourceforge.net/projects/gallery/files/gallery3/3.0.2/gallery-3.0.2.zip/download");
        FileUtils.copyURLToFile(url, new File("test.zip"));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   }
 }

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");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文