在以前的文章中提到了文件的上传,有同学询问基于resteasy下载的实现,故本篇文章主要描述如何实现下载
配置和框架的搭建都不罗嗦了,请查看以前的文章
先贴出下载的代码
/**
* 下载对象
* @param bucketName
* @param objectName
* @param request
* @return
*/
@Path(value = "/{bucketName}/{objectName}")
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadObject(@PathParam(value = "bucketName") String bucketName,@PathParam(value = "objectName") String objectName,@Context HttpServletRequest request)
{
BucketObject bucketObject = objectManager.downloadObject(request.getHeader(CommonConstant.USER_TOKEN),CommonsUtil.generateSummary(request), bucketName, objectName);
//直接返回输出流
return Response.ok(new BigFileOutputStream(bucketObject.getDataStream())).build();
这段代码比较简单:重点关注响应的实体类BigFileOutputStream,其传入的构造参数是一个输入流
该类的具体实现如下,其实关键的需要实现javax.ws.rs.core.StreamingOutput类,重写其write方法,将你得数据输出到write方法中得输出流中即可,resteasy框架会将其流直接输出到客户端
/**
* 实现用于直接响应一个输出流
* @author maomao
*
*/
public class BigFileOutputStream implements javax.ws.rs.core.StreamingOutput {
private InputStream inputStream;
public BigFileOutputStream(){}
public BigFileOutputStream(InputStream inputStream)
{
this.inputStream = inputStream;
}
@Override
public void write(OutputStream output) throws IOException,
WebApplicationException {
// TODO Auto-generated method stub
IOUtils.copy(inputStream, output);
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
客户端调用代码如下
//下载文件
private void downLoadObject()
{
String bucket = "EBOXTEST123456"; //上传的桶
String objectName = "wangpan114daitupian.png"; //上传的对象名
String urlResouce = bucket + "/" + objectName;
String baseURL = "http://10.28.1.37:8080/application_interface_manager/platform/";
String hostURL = baseURL + urlResouce ;
String httpMethod = "GET";
String requestDate = new Date().toString();
try{
File localFile = new File("x:\\1142.png");
//生成摘要用于签名
String summary = httpMethod + "\n" // create StringToSign
+ "\n" // no Content-Md5 Filed
+ "\n" // no Content-Type Filed
+ requestDate + "\n" // Date Filed---must
+ urlResouce; //must
//创建客户端签名
String clientToken = new CreateSignTokenImpl().getToken(summary,secretKey);
HttpURLConnection urlConnection =
(HttpURLConnection) (new URL(hostURL)).openConnection();
urlConnection.setRequestProperty("Token", "jingdong "+accessKey+":"+clientToken);
urlConnection.setRequestProperty("Date", requestDate);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod(httpMethod);
InputStream is = urlConnection.getInputStream();
// FileUtils.copyInputStreamToFile(is, localFile);
try {
OutputStream op = new FileOutputStream(localFile);
byte [] bts = new byte[1024];
int n = 0;
while(-1 != (n = is.read(bts)))
{
op.write(bts, 0, n);
}
// op.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(urlConnection.getResponseCode());
}
catch(Exception e)
{
e.printStackTrace();
}
}
其实看着复杂,实际上很多代码不重要(偷懒把项目客户端调用代码直接copy过来了),关键就是从HttpUrlConnect处获取输入流(即是服务端输出流输出的数据),将其写入到文件即可
很简单,如有问题欢迎加QQ 316853417咨询