当我下载文件时,它总是抛出这个异常:
org.apache.http.ConnectionClosedException:Content-Length分隔邮件正文的过早结束(预期:210846;收到:0
我的代码:
String fileid=attachment.getBoxfileid();
String sha1=attachment.getSha1();
String filename=attachment.getFilename();
final String clientid=ToolsUtils.getBOXcomConfig().get(ToolsUtils.CLIENTID);
final String clientsecret=ToolsUtils.getBOXcomConfig().get(ToolsUtils.CLIENTSECRET);
BoxOAuthToken authToken=this.boxuploadService.getTokenByClientId(clientid);
BoxClient boxClient=new BoxClient(clientid,clientsecret,null,null,new BoxConfigBuilder().build());
boxClient.authenticate(authToken);
boxClient.addOAuthRefreshListener(new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData authData) {
boxuploadService.updateBoxAccessToken(clientid,authData.getAccessToken(),authData.getRefreshToken());
}
});
BoxDefaultRequestObject obj = new BoxDefaultRequestObject();
obj.getRequestExtras().setIfMatch(sha1);
DownloadFileRequest downloadFileRequest=new DownloadFileRequest(new BoxConfigBuilder().build(), new BoxJSONParser(new BoxResourceHub()), fileid, obj);
out = response.getOutputStream();
InputStream inputStream=boxClient.getFilesManager().downloadFile(fileid, obj);
byte[] buffer = new byte[1024];
response.setContentType("application/octet-stream" );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + filename + "\"" );
while((readCount = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, readCount);
}
out.flush();
答案 0 :(得分:0)
看起来读取部分出错了,在调用downloadFile(fildId,requestObject)时是否捕获到任何异常?
我的代码如下所示:
try {
BoxDefaultRequestObject requestObject = new BoxDefaultRequestObject();
inputStream = boxClient.getFilesManager().downloadFile(fileId,
requestObject);
} catch (BoxRestException e) {
e.printStackTrace();
} catch (BoxServerException e) {
e.printStackTrace();
} catch (AuthFatalFailureException e) {
e.printStackTrace();
}
您可以通过这种方式检查InputStream对象是否正确生成。