使用spring boot从外部url下载文件
Posted
技术标签:
【中文标题】使用spring boot从外部url下载文件【英文标题】:Download file from external url using spring boot 【发布时间】:2021-11-24 03:01:41 【问题描述】:我想下载来自外部 url 的文件。但是我遇到了错误。有没有更好的方法来实现这一点?
这是我的代码:
public ResponseEntity downloadAsset() throws IOException
final String username = "abd";
final String password = "rewf";
String url = "https://www.clickdimensions.com/links/TestPDFfile.pdf";
String plainCreds = username + ":" + password;
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.setHeader("Authorization", "Basic " + base64Creds);
HttpResponse response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode() == 200)
System.out.println("response:: " + response.getEntity());
return ResponseEntity.ok()
.contentLength(response.getEntity().getContentLength())
.body(response.getEntity().getContent());
return ResponseEntity.ok().build();
我遇到以下错误:
There was an unexpected error (type=Internal Server Error, status=500).
No converter found for return value of type: class org.apache.http.conn.EofSensorInputStream
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class org.apache.http.conn.EofSensorInputStream
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:230)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:226)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:124)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
【问题讨论】:
【参考方案1】:看起来这部分代码正在引发异常.body(response.getEntity().getContent());
。 getContent()
方法返回 InputStream 并且您不能只是简单地将它放在响应正文中。你需要妥善处理(见:https://www.baeldung.com/convert-input-stream-to-string 或http://tutorials.jenkov.com/java-io/inputstream.html)。
【讨论】:
以上是关于使用spring boot从外部url下载文件的主要内容,如果未能解决你的问题,请参考以下文章