java中从http请求获取文件

发布于 2024-07-07 04:55:44 字数 290 浏览 8 评论 0原文

如何调用 url 来处理结果?

我有一个独立的报告 servlet,我链接到该报告以获取报告。 我现在想通过电子邮件发送这些报告,如果我在浏览器中执行此操作,我可以只使用 xhttprequest,并处理结果 - 我基本上想在 Java 中做同样的事情,但我不知道如何去做它。

更新:我希望从 url 获取一个文件(无论是 pdf 还是 html 等)。

更新:这将纯粹在服务器上运行 - 没有触发电子邮件的请求,而是预定的电子邮件。

How do I call a url in order to process the results?

I have a stand-alone reporting servlet which I link to for reports. I want to email these reports now, if I were doing this in the browser, I could just use an xhttprequest, and process the results - I basically want to do the same thing in Java, but I'm not sure how to go about it.

UPDATE: I'm looking to get a file back from the url (whether that be a pdf or html etc).

UPDATE: This will be running purely on the server - there is no request that triggers the emailing, rather it is a scheduled email.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

相权↑美人 2024-07-14 04:55:44
public byte[] download(URL url) throws IOException {
    URLConnection uc = url.openConnection();
    int len = uc.getContentLength();
    InputStream is = new BufferedInputStream(uc.getInputStream());
    try {
        byte[] data = new byte[len];
        int offset = 0;
        while (offset < len) {
            int read = is.read(data, offset, data.length - offset);
            if (read < 0) {
                break;
            }
          offset += read;
        }
        if (offset < len) {
            throw new IOException(
                String.format("Read %d bytes; expected %d", offset, len));
        }
        return data;
    } finally {
        is.close();
    }
}

编辑:清理代码。

public byte[] download(URL url) throws IOException {
    URLConnection uc = url.openConnection();
    int len = uc.getContentLength();
    InputStream is = new BufferedInputStream(uc.getInputStream());
    try {
        byte[] data = new byte[len];
        int offset = 0;
        while (offset < len) {
            int read = is.read(data, offset, data.length - offset);
            if (read < 0) {
                break;
            }
          offset += read;
        }
        if (offset < len) {
            throw new IOException(
                String.format("Read %d bytes; expected %d", offset, len));
        }
        return data;
    } finally {
        is.close();
    }
}

Edit: Cleaned up the code.

撑一把青伞 2024-07-14 04:55:44

如果您的目的是在 servlet 执行时运行另一个资源而不将控制权转移到其他资源,您可以尝试使用 include(request, response)。

RequestDispatcher dispatcher =
   getServletContext().getRequestDispatcher("/url of other resource");
if (dispatcher != null)
   dispatcher.include(request, response);
} 

您可以将其放在 servlet 上,其他资源的结果将包含在您的 servlet 中。

编辑:由于您希望取回文件,因此该解决方案也适用于该问题。

If the intention is to run another resource while your servlet is executing with out transferring control to the other resource you can try using include(request, response).

RequestDispatcher dispatcher =
   getServletContext().getRequestDispatcher("/url of other resource");
if (dispatcher != null)
   dispatcher.include(request, response);
} 

You may put this on a servlet and the result of the other resource is included on your servlet.

EDIT: Since you are looking to get a file back then this solution works for that too.

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