首页 javaWEB java批量打包下载springmvc

java批量打包下载springmvc

/** * 下载 报告资料 * @param request * @param response * @param re…

QQ图片20150922155843

/**
	 * 下载 报告资料
	 * @param request
	 * @param response
	 * @param reportId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/downReport")
	public String downReport(HttpServletRequest request,HttpServletResponse response,long reportId) throws IOException{

		COM_REPORT_PATH=rb.getString("COM_REPORT_PATH");
		User user= userManagerService.getCurrentUser();
		if(user==null||user.getOid()==0){
			return null;
		}
		Report report = reportoService.getReportByOid(3473408l);

		    String tmpFileName = "report.zip";
	        byte[] buffer = new byte[1024];
	        String strZipPath = COM_REPORT_PATH+"/"+user.getOid()+"/"+report.getOid()+"/"+tmpFileName;
	        //FilePath + tmpFileName;
	        try {
	            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
	                    strZipPath));
	            // 需要同时下载的两个文件result.txt ,source.txt
	            String []filePath=report.getZipAddress().split(",");
	            File[] file1 =new File[filePath.length] ;

	            for(int i=0;i<filePath.length;i++){
	            	file1[i]=new File(filePath[i]);
	            }



	            for (int i = 0; i < file1.length; i++) {
	                FileInputStream fis = new FileInputStream(file1[i]);
	                out.putNextEntry(new ZipEntry(file1[i].getName()));
	                //设置压缩文件内的字符编码,不然会变成乱码
	                out.setEncoding("GBK");
	                int len;
	                // 读入需要下载的文件的内容,打包到zip文件
	                while ((len = fis.read(buffer)) > 0) {
	                    out.write(buffer, 0, len);
	                }
	                out.closeEntry();
	                fis.close();
	            }
	            out.close();
	            this.downloadFile(strZipPath,response);
	        } catch (Exception e) {
	         e.printStackTrace();
	        }
	        return null;
	}



    public void downloadFile(String fileName,HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");

        response.setHeader("Content-Disposition", "attachment;fileName=report.zip");
        try {
            File file=new File(fileName);
            System.out.println(file.getAbsolutePath());
            InputStream inputStream=new FileInputStream(file);
            OutputStream os=response.getOutputStream();
            byte[] b=new byte[1024];
            int length;
            while((length=inputStream.read(b))>0){
                os.write(b,0,length);
            }
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//D:/report/1835009/3473408/report_2.zip,D:/report/1835009/3473408/report_2.zip,D:/report/1835009/3473408/report_3.zip,D:/report/1835009/3473408/report_4.zip,D:/report/1835009/3473408/report_5.zip

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

为您推荐

nodejs 整理记录

nodejs 整理记录

下载包 https://blog.csdn.net/m0_59878114/article/details/120274...
websocket测试html

websocket测试html

<!DOCTYPE html> <html> <head> <meta cha...
bigdemical两个数比较大小

bigdemical两个数比较大小

/*int result = bigdemical1.compareTo(bigdemical2) result = -...
Beetl2.7 中文文档

Beetl2.7 中文文档

Beetl目前版本是2.7.23,相对于其他java模板引擎,具有功能齐全,语法直观,性能超高,以及编写的模板容易维护等...
纯CSS实现多个便签在一行展示,拖动滚动

纯CSS实现多个便签在一行展示,拖动滚动

div <h2>请注意需要在移动端预览,PC端拖拽无效果</h2> <div class=...
返回顶部