restful 文件下载

来源:互联网 发布:淘宝女装精修图教程 编辑:程序博客网 时间:2024/06/02 04:38

@GET
@Path(“/download”)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downLoadFile(
@QueryParam(“fileName”) String fileName){
ShardedJedis jedis = null;
Response response =null;
ObjectInputStream inputStream=null;
try{
jedis = redisDataSource.getRedisClient();
byte [] bytes = jedis.get(fileName.getBytes());//从redis中获取文件流
inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
ResponseBuilder responseBuilder = Response.ok(inputStream);
responseBuilder.type(“application/x-msdownload”);
responseBuilder.header(“Content-Disposition”, “attachment; filename=”
+ URLEncoder.encode(fileName, “UTF-8”));
//responseBuilder.header(“Content-Length”, Long.toString(fileLength));
response = responseBuilder.build();
}catch(Exception e){
e.printStackTrace();
}
return response;
}

原创粉丝点击