如何在Spring控制器中从URL服务器inputStream

发布于 2025-01-06 19:48:57 字数 723 浏览 0 评论 0原文

我正在尝试构建一个 Spring 控制器来从 url 提供文件:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<byte[]> getFile () throws IOException {
     CommonHttpClient client = new CommonHttpClient();
     URL url = new URL("http://www.google.com");
     InputStream stream = url.openStream();
     final HttpHeaders headers = new HttpHeaders();
     headers.add("Content-Type", "text/html");
     return new ResponseEntity<byte[]>(IOUtils.toByteArray(stream), headers, HttpStatus.CREATED);
}

我在 bean 配置中的 AnnotationMethodHandlerAdaptor 中有 ByteArrayHttpMessageConverter 。

但是,当我调用此页面时,我收到了无意义的字符串,例如 “PHBYzT5QB……” url 肯定可以访问并且没有抛出 IOException。 我在这里缺少什么?

I am trying to build a Spring controller to serve a file from a url:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<byte[]> getFile () throws IOException {
     CommonHttpClient client = new CommonHttpClient();
     URL url = new URL("http://www.google.com");
     InputStream stream = url.openStream();
     final HttpHeaders headers = new HttpHeaders();
     headers.add("Content-Type", "text/html");
     return new ResponseEntity<byte[]>(IOUtils.toByteArray(stream), headers, HttpStatus.CREATED);
}

I have ByteArrayHttpMessageConverter in my AnnotationMethodHandlerAdaptor in bean configuration.

However, when I call this page, I am getting nonsensical strings like
"PHBYzT5QB...."
The url is definitely reachable and no IOException were thrown.
What am I missing here?

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

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

发布评论

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

评论(2

送舟行 2025-01-13 19:48:57

我认为你在这里混合了一些东西。如果您想要提供的文件在本地文件系统上可用,那么您不需要从 URL 读取。如果您使用 HttpResponse 参数定义方法签名,您将能够获取 OutputStream 并写入它。不需要转换器 - 只需从一个流(文件)读取并循环写入另一个流(文件)即可。在响应中设置正确的内容类型标头也很重要。

@RequestMapping...
public void getFile(HttpResponse resp) throws IOException {
  InputStream is = ... // get InputStream from your file
  resp.setContentType("text/html"); // or whatever is appropriate for your file
  OutputStream os = resp.getOutputStream();
  // now read from one stream and write to the other
  byte[] buffer = new byte[1024];
  int len = in.read(buffer);
  while (len != -1) {
    out.write(buffer, 0, len);
    len = in.read(buffer);
  }
}

I think you're mixing few things here. If the file you want to serve available on your local file system then you don't need to read from URL. If you define your method signature with HttpResponse parameter you will be able to get OutputStream and write to it. No converters should be necessary - just read from one stream (file) and write to the other in a loop. It is also important to set correct content type header in response.

@RequestMapping...
public void getFile(HttpResponse resp) throws IOException {
  InputStream is = ... // get InputStream from your file
  resp.setContentType("text/html"); // or whatever is appropriate for your file
  OutputStream os = resp.getOutputStream();
  // now read from one stream and write to the other
  byte[] buffer = new byte[1024];
  int len = in.read(buffer);
  while (len != -1) {
    out.write(buffer, 0, len);
    len = in.read(buffer);
  }
}
若沐 2025-01-13 19:48:57

我认为这是 BASE 编码的字节数组

i think this is BASE encoded byte array

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