restful 文件下载

@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;
}

你可能感兴趣的:(restful 文件下载)