2012-08-30 41 views
0

第三方服务正在使用HttpClient(3.1)来获取我通过Jersey(在tomcat上)提供的url。它将错误抛诸脑后。违反协议:块大小中意外的单个换行符

这是我所服务的URL:

@Path("somepath") 
@GET 
@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response download(@Context UriInfo uriInfo) { 
    try { 
     URL url = // find the actual URL (a file) 
     InputStream stream = url.openStream(); 
     return Response.ok(stream).build(); 
    } catch (IOException e) { 
     return Responses.notFound().build(); 
    } 
} 

是否有办法来解决此问题?

+0

确保Jersey使用CRLF('\ r \ n')终止行而非空行。 – oldrinb

+0

你知道吗? (顺便说一句,我认为这是一个Tomcat问题)。 – IttayD

回答

0

最后我做这样的事情:

if (url.getScheme().equalsIgnoreCase("file")) { 
    File file = new File(url.toURI()); 
    return Response.ok(file).header("Content-Length", file.length()).build(); 
} else { 
    return Response.seeOther(url); 
} 

这对我的使用情况下工作。

一些注意事项:

  • 就返回文件对象的工作,如果该文件的大小是小(约2G的地方),但没有较大的文件。我假设由于整数溢出而以某种方式分块长度编码中断。
  • 尝试返回StreamingOutput(在另一个问题中提出)不适用于大文件。
相关问题