文件处理(文件打包、文件下载、文件上传)

文件处理(文件打包、文件下载、文件上传)

1. 通过文件路劲将文件打成zip包

  1. 通过传入一个目标文件路径文件,和一个打包后的位置
public static void compress(String srcPathName, String decZipFile) {
		compress(srcPathName, decZipFile, true);
	}
  1. compress方法实现
public static void compress(String srcPathName, String decZipFile, boolean isDeleteSrcFile) {
   try {
      File srcFile = new File(srcPathName);
      if (!srcFile.exists()) {
         log.error(srcPathName + "不存在");
         throw new RuntimeException(srcPathName + "不存在");
      }
      File zipFile = new File(decZipFile);
      Project prj = new Project();
      Zip zip = new Zip();
      zip.setProject(prj);
      zip.setDestFile(zipFile);
      FileSet fileSet = new FileSet();
      fileSet.setProject(prj);
      fileSet.setFile(srcFile);
      zip.addFileset(fileSet);
      zip.execute();
      if(isDeleteSrcFile) {
         srcFile.delete();
      }
   } catch (Exception e) {
      log.error(e.getMessage(), e);
   }
}
  1. 参数说明

s r c P a t h N a m e = 目标文件位置(可以是文件,也可以是文件夹) d e c Z i p F i l e = 文件解压后的位置 + 以 . z i p 结尾的文件名称 srcPathName = 目标文件位置(可以是文件,也可以是文件夹) decZipFile = 文件解压后的位置 +以.zip结尾的文件名称 srcPathName=目标文件位置(可以是文件,也可以是文件夹)decZipFile=文件解压后的位置+.zip结尾的文件名称

2. 将文件上传到系统目录中

读取文件,会返回一个byte类型的数组
public CommonResult<byte[]> readFile(String filePath, Integer accessType) {

		if(StrUtil.isBlank(filePath)) {
			return CommonResult.error(OssErrorCodeConstants.OSS_FILE_ISNULL);
		}
		String rootPath = getRootPath(filePath, accessType); // 文件全路径
		BufferedInputStream bis = null;
		byte[] bytes = null;
		try {
			File file = new File(rootPath);
			if(!file.exists()) {
				return CommonResult.error(OssErrorCodeConstants.OSS_DOWN_FILENOTEXISTS);
			}
			bis = new BufferedInputStream(new FileInputStream(file));
			bytes = new byte[bis.available()];
			bis.read(bytes, 0, bytes.length);
		}catch(Exception ex) {
			log.error("读本地文件错误: {}", ex.getMessage(), ex);
			
		}finally {
			if(bis!=null) {
				try {
					bis.close();
				}catch(Exception ex) {}
			}
		}
		return CommonResult.ok(bytes);
	}
文件上传时可以传一个input流也可以是一个byte[]

1.通过input流方式上传

	public CommonResult<?> saveFile(InputStream is, String filePath, Integer accessType) {
		if(is==null || StrUtil.isBlank(filePath)) {
			return CommonResult.error(OssErrorCodeConstants.OSS_FILE_ISNULL);
		}
		FileOutputStream fw = null;
		try {
			String rootPath = getRootPath(filePath, accessType);
			File rootFile = new File(rootPath);
			File rootDir = rootFile.getParentFile();
			if(!rootDir.exists()) {
				rootDir.mkdirs();
			}
			fw = new FileOutputStream(rootFile);
			byte[] buffer = new byte[8192];
			int length;
			while ((length = is.read(buffer)) != -1) {
				fw.write(buffer, 0, length);
			}
			fw.close();
			fw = null;
		}catch(Exception ex) {
			log.error("写本地文件错误: {}", ex.getMessage(), ex);
			return CommonResult.error(OssErrorCodeConstants.OSS_UNKNOWN);
		}finally{
			if(fw!=null) {
				try {
					fw.close();
				}catch(Exception ex) {}
			}
		}
		return CommonResult.ok(null);
	}

2.通过byte[]方式传输

public CommonResult<?> saveFile(byte[] content, String filePath, Integer accessType) {
		if(content==null || StrUtil.isBlank(filePath)) {
			return CommonResult.error(OssErrorCodeConstants.OSS_FILE_ISNULL);
		}
		FileOutputStream fw = null;
		try {
			String rootPath = getRootPath(filePath, accessType);
			File rootFile = new File(rootPath);
			File rootDir = rootFile.getParentFile();
			if(!rootDir.exists()) {
				rootDir.mkdirs();
			}
			fw = new FileOutputStream(rootFile);
			fw.write(content);
			fw.close();
			fw = null;
		}catch(Exception ex) {
			log.error("写本地文件错误: {}", ex.getMessage(), ex);
			return CommonResult.error(OssErrorCodeConstants.OSS_UNKNOWN);
		}finally{
			if(fw!=null) {
				try {
					fw.close();
				}catch(Exception ex) {}
			}
		}
		return CommonResult.ok(null);
	}

补充 – getRootPath 方法实现

private String getRootPath(String filePath, Integer accessType) {
		String rootPath = localOssConfig.getPrivatePath();
		if(AccessTypeEnum.PUBLIC_READ.getCode().equals(accessType)) {
			rootPath = localOssConfig.getPublicPath();
		}
		if(StrUtil.isBlank(rootPath)) {
			return filePath;
		}
		if(!rootPath.endsWith("/")) {
			rootPath = rootPath + "/";
		}
		if(filePath.startsWith("/")) {
			filePath = filePath.substring(1);
		}
		return rootPath + filePath;
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值