Java下载文件夹转换成zip

需求:下载格式为压缩包下面存放多个文件夹,每个文件夹存放多个文件。

思路:文件上传到minio,上传的路径设置好,就按照你想要导出的格式存放文件,下载时先下载到服务上,从服务上下载到本地,下载完毕以后删除服务上的文件夹。

一、上传文件到minio

1、Controller层业务展示

/**
	  * 文件上传统一方法
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @AutoLog(value = "文件上传统一方法")
	 @ApiOperation(value = "文件上传统一方法", notes = "文件上传统一方法")
	 @PostMapping(value = "/upload")
	 public Result<?> uploads(HttpServletRequest request, HttpServletResponse response, String auditReport, String biz) {
		 Result<?> result = new Result<>();
		 String savePath = "";
//		 String bizPath = request.getParameter("biz");
		 String bizPath = biz;
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
		 if (oConvertUtils.isEmpty(bizPath)) {
			 if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) {
				 //未指定目录,则用阿里云默认目录 upload
				 bizPath = "upload";
				 //result.setMessage("使用阿里云文件上传时,必须添加目录!");
				 //result.setSuccess(false);
				 //return result;
			 } else {
				 bizPath = "";
			 }
		 }
		 if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
			 //update-begin-author:lvdandan date:20200928 for:修改JEditor编辑器本地上传
			 savePath = this.uploadLocal(file, bizPath);
			 //update-begin-author:lvdandan date:20200928 for:修改JEditor编辑器本地上传
			 /**  富文本编辑器及markdown本地上传时,采用返回链接方式
			  //针对jeditor编辑器如何使 lcaol模式,采用 base64格式存储
			  String jeditor = request.getParameter("jeditor");
			  if(oConvertUtils.isNotEmpty(jeditor)){
			  result.setMessage(CommonConstant.UPLOAD_TYPE_LOCAL);
			  result.setSuccess(true);
			  return result;
			  }else{
			  savePath = this.uploadLocal(file,bizPath);
			  }
			  */
		 } else {
			 //update-begin-author:taoyan date:20200814 for:文件上传改造
			 return pmMaterialMessageService.savePaths(file, bizPath, uploadType, auditReport);

			 //update-end-author:taoyan date:20200814 for:文件上传改造
		 }
		 if (oConvertUtils.isNotEmpty(savePath)) {
			 result.setMessage(savePath);
			 result.setSuccess(true);
		 } else {
			 result.setMessage("上传失败!");
			 result.setSuccess(false);
		 }
		 return result;
	 }

	 /**
	  * 本地文件上传
	  *
	  * @param mf      文件
	  * @param bizPath 自定义路径
	  * @return
	  */
	 private String uploadLocal(MultipartFile mf, String bizPath) {
		 try {
			 String ctxPath = uploadpath;
			 String fileName = null;
			 File file = new File(ctxPath + File.separator + bizPath + File.separator);
			 if (!file.exists()) {
				 file.mkdirs();// 创建文件根目录
			 }
			 String orgName = mf.getOriginalFilename();// 获取文件名
			 orgName = CommonUtils.getFileName(orgName);
			 if (orgName.indexOf(".") != -1) {
				 fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
			 } else {
				 fileName = orgName + "_" + System.currentTimeMillis();
			 }
			 String savePath = file.getPath() + File.separator + fileName;
			 File savefile = new File(savePath);
			 FileCopyUtils.copy(mf.getBytes(), savefile);
			 String dbpath = null;
			 if (oConvertUtils.isNotEmpty(bizPath)) {
				 dbpath = bizPath + File.separator + fileName;
			 } else {
				 dbpath = fileName;
			 }
			 if (dbpath.contains("\\")) {
				 dbpath = dbpath.replace("\\", "/");
			 }
		
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值