JAVA文本及图片打压缩包下载

public void downupload(User u ,HttpServletRequest request, HttpServletResponse response){
 		List<User> list = systemService.findUser(u);
		String name = "人员信息";
		String filenameTemp = name + ".txt";
		File file = new File(filenameTemp);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			 //获得输出流
	        OutputStreamWriter bos =new OutputStreamWriter(fos);
	        BufferedWriter bw = new BufferedWriter(bos);
	        bw.write("人员编号,姓名,性别,所属单位 ");
	        bw.newLine();
	        for (User user : list) {
	    		if(user.getSex() != null && user.getSex().equals("1")){
	    			user.setSex("男");
	    		}
	    		if(user.getSex() != null && user.getSex().equals("2")){
	    			user.setSex("女");
	    		}
	        	bw.write( user.getNum() +","+ user.getName() +","+ user.getSex() +","+ user.getOffice().getName());
	        	bw.newLine();
	            bw.flush();
			}
	        bw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        
        Date date = new Date();
        DateFormat df=new SimpleDateFormat("MMddHHmmss");
        String time = df.format(date);   
            byte[] buffer = new byte[1024];
            FileOutputStream fo;
            FileInputStream fs = null;
            ZipOutputStream zos = null;
            try {
				fo = new FileOutputStream(name+time+".zip");
	            zos = new ZipOutputStream(fo);
	            String path = request.getSession().getServletContext().getRealPath("/"); 
	            for(int i = 0; i < 2; i++){
	            	FileInputStream fis = null;
	            	if(i == 0){
	            		fis = new FileInputStream(file);
	            		zos.putNextEntry(new ZipEntry(file.getName()));
	            		int length;
	            		while ((length = fis.read(buffer)) > 0) {
	            			zos.write(buffer, 0, length);
	            		}
	                	fis.close();
	            	}else{
	            		for (User user : list) {
	            			if(user.getPhotoPath() != null && user.getPhotoPath().length() > 0){
	            				String upath = user.getPhotoPath().replace("/xljdgl","");
	            				File f = new File(path + upath);
	        				    File newFile = new File(path + user.getNum()+".jpg");
	        				    f.renameTo(newFile); //改名
	         				    fs = new FileInputStream(newFile);
	        				    zos.putNextEntry(new ZipEntry(newFile.getName()));
	        				    int length;
	        				    while ((length = fs.read(buffer)) > 0) {
	        					    zos.write(buffer, 0, length);
	        				    }
            				
	            			}
	            			
						}
	            	}
	            }
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					zos.closeEntry();
					if(fs != null){
					    fs.close();
				    }
					zos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
      
        InputStream bis;
		try {
			bis = new BufferedInputStream(new FileInputStream(new File(name+time+".zip")));
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition", "attachment; filename="+Encodes.urlEncode(name+time+".zip"));
			BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
			int len = 0;
			while ((len = bis.read()) != -1) {
				out.write(len);
				out.flush();
			}
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

猜你喜欢

转载自blog.csdn.net/weixin_40452506/article/details/105490878