文章目录
文件处理(文件打包、文件下载、文件上传)
1. 通过文件路劲将文件打成zip包
- 通过传入一个目标文件路径文件,和一个打包后的位置
public static void compress(String srcPathName, String decZipFile) {
compress(srcPathName, decZipFile, true);
}
- 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);
}
}
- 参数说明
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;
}
}