angularjs 和jersey 实现文件下载

$http({
method : "post",
url : Constant.prefixUrl+"/customerQueryCount/exportMonthReport",
timeout : Constant.timeout,
responseType: "blob"   //注意此参数
}).success(function(data, status, headers, config){
var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});  
if(blob.size>0){
var fileName = $rootScope.currentAdminUserId +""+ (new Date()).getTime();
var a = document.createElement("a");
document.body.appendChild(a);
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.click();
}else{
deferred.resolve("下载报表失败");
}
}).error(function(data, status, headers, config){
commonService.goLoginPage(status);

});

后台:

    @POST
    @Path("/export2")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public byte[] export2(@RequestBody String params, @Context HttpServletResponse response) {

        File file = new File(filePath + filePrefix + ".xlsx");

        FileInputStream fis;
        byte[] b =    null;
        try {
            fis = new FileInputStream(file);
            b = new byte[fis.available()];
            fis.read(b);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        response.setHeader("Content-Disposition", "attachment;filename=" + fu + ".xlsx" );// 为文件命名
        response.addHeader("content-type", "application/pdf");
        return b;



猜你喜欢

转载自blog.csdn.net/qq_33363618/article/details/79581646