我必须将一个文件从一个java类发送到一个Spring rest web服务。用例:从客户端,这可以选择服务器、服务器上的文件以及保存文件的位置。在服务器上,我必须检索文件(从客户机选择)并发送到客户机。目前我使用字节数组,但当文件超过50MB时,我会收到堆异常,而我将multipartfile
从web页面发送到控制器,即使文件超过500MB,我也使用相同的方法。在客户端,我使用这个spring方法:
Response responseStatus = restTemplate.postForObject(serverIp + "ATS/client/file/?toStorePath={toStorePath}", responseSend, Response.class, toStorePath);
@Override
@RequestMapping(value = "/file",produces=MediaType.APPLICATION_OCTET_STREAM, method = RequestMethod.GET)
public javax.ws.rs.core.Response getAcquisition(@RequestParam(value="path", required = true) String path){
try(InputStream is = new FileInputStream(path)){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[4096];
while ((len = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println("Server size: " + baos.size());
return javax.ws.rs.core.Response.ok(baos).build();
}catch(Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
return null;
}
@Override
public Response getFile(String serverIp, String toStorePath, String filePath){
FileOutputStream out = null;
InputStream is = null;
try{
Client client = ClientBuilder.newClient();
String url = serverIp + "ATS/client/file/";
javax.ws.rs.core.Response response = client.target(url).queryParam("path",filePath).request().get();
String location = toStorePath + "prova.zip";
out = new FileOutputStream(location);
is = (InputStream)response.getEntity();
int len = 0;
byte[] buffer = new byte[4096];
while((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
return new Response(false, false,"Your required file has been correctly written!" , null);
}catch(Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in FileServicesImpl::getFile :" + errorResponse.getStacktrace());
return new Response(false, false,"Error while write file into local file system!" , errorResponse);
}finally{
try{
if (out != null){
out.flush();
out.close();
}
if (is!=null)is.close();
}catch (Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in FileServicesImpl::getFile :" + errorResponse.getStacktrace());
return new Response(false, false,"Error during stream closeing!" , errorResponse);
}
}
这是服务器上的异常
dic 04,2015 5:00:54 PM org.apache.catalina.core.standardWrapperValve在路径[/ats]的上下文中为servlet[dispatcher]调用grave:servlet.service()引发异常[请求处理失败;嵌套异常是org.thymeleaf.exceptions.templateInputException:错误解析模板“client/file”,模板可能不存在或无法被任何配置的模板解析器访问]根源是org.thymeleaf.exceptions.templateInputException:错误解析模板“client/file”,模板可能不存在或无法被templateEngine.process(templateEngine.java:1104)在org.thymeleaf.templateEngine.process(templateEngine.java:1060)在org.thymeleaf.templateEngine.process(templateEngine.java:1011)在org.thymeleaf.spring4.view.thymeleaf.renderFragment(thymeleafview.java:335)在org.thymeleaf.spring4.view.thymel在org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:190)在org.springframework.web.servlet.dispatcherservlet.processDispatchResult(dispatcherservlet.java:1244)在org.springframework.web.servlet.dispatcherservlet.java:1027)在org.springframework.web.servlet.dispatcherservlet.doDispatch(let.doget(frameworkservlet.java:861)在javax.servlet.http.httpservlet.service(httpservlet.java:622)在org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:846)在javax.servlet.http.httpservlet.java:729)在org.apache.catalina.core.applicationfilterchain.internaldoFilter(applicationfilterchain.java:20)在6)在org.apache.tomcat.websocket.server.wsfilter.doFilter(wsfilter.java:52),在org.apache.catalina.core.applicationFilterChain.InternaldoFilter(applicationFilterChain.java:239),在org.apache.catalina.core.applicationFilterChain.doFilter(applicationFilterChain.java:206),在org.apache.catalina.core.standardWrapperValve.invoke authenticatorbase.invoke(authenticatorbase.java:502)在org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142)在org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.invoke(errorreportvalve.java:79)在org.apache.catalina.valves.abractAccessLogvalve.invoke(apache.coyote.http11.abstractHttp11proces在org.apache.coyote.abstractProtocol$abstractConnectionHandler.process(abstractProtocol.java:673),在org.apache.tomcat.util.net.nioEndpoint$socketProcessor.dorun(NioEndpoint.java:1526),在org.apache.tomcat.util.net.nioEndpoint$socketProcessor.run(NioEndpoint.java:1482),在:617)在org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)在java.lang.thread.run(thread.java:745)
500MB以上Rest。我不喜欢这个主意。
使用webservice只向您的文件发送一个下载链接,并让您的HTTP服务器(如Apache或NGINX)处理下载请求并提供文件。