admin管理员组

文章数量:1533913

一、声明

注意参数要有HttpServletResponse response并设置参数

response.setContentType("application/application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment;filename=" + fileName);

@RequestMapping(value = "downloadZip", method = RequestMethod.GET)
    public void downloadZip(HttpServletResponse response,String id) throws Exception {
        String fileName=workCardPhotoFileService.downloadZipFile(id);
        if (StringUtils.isNotEmpty(fileName)){
            response.setContentType("application/application/vnd.ms-excel");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName);
            download(response.getOutputStream(),fileName);
        }


    }
 public void download(OutputStream os, String fileName) throws IOException {
        //获取服务器文件
        File file = new File("/Users/Desktop/download/workcardphoto/"+fileName);

        InputStream ins = new FileInputStream(file);
        byte[] b = new byte[1024];
        int len;
        while((len = ins.read(b)) > 0){
            os.write(b,0,len);
        }
        os.flush();
        os.close();
        ins.close();
    }

结束~感谢观看

本文标签: 浏览器服务器文件Java