前言:最近遇到一个开发的问题,就是上传了一个Gzip文件,需要对其解压缩,并将解压后的文件上传至Minio里面去,左思右想,搞出一个方法,可能不太成熟,有能修改的地方,欢迎大家指出!
废话不多说,咱们马上开干!直接上码
import io.minio.*;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.GZIPInputStream;
/**
* @author XMLee
* @date 2022/4/20 9:57
*/
@RestController
@RequestMapping(“/upload”)
@Slf4j
public class CollapseController {
@Autowired
private MinioClient minioClient;
@Value("${minio.S3_BUCKET_NAME_CRASHPAD:crashpad}")
private String crashpadBucket; //这点对应自己Minio的Bucket
private final String CONTENT_ENCODING = "Content-Encoding";
private final String GZIP = "gzip";
private final String GUID = "guid";
private final String MULTIPART_BOUNDARY = "boundary";
private final String CONTENT_DISPOSITION = "Content-Disposition";
private final String DMP_FILE_KEY = "upload_file_minidump";
private final String DMP = ".dmp";
private final String JSONS = ".json";
@PostMapping("/save")
@ResponseBody
public RestResponse saveCollapseLog(MultipartHttpServletRequest req, HttpServletResponse response, HttpServletRequest request) {
//判断这个上传的是不是gzip,根据自己业务
String lowerCaseGzip = request.getHeader(CONTENT_ENCODING).toLowerCase();
if (GZIP.equals(lowerCaseGzip)) {
String guid = request.getParameter(GUID);
String prefix = null;
if (!StringUtils.hasText(guid)) {
return RestResponse.fail("guid not exist ! ! ! ");
}
prefix = getObjectPrefix(guid);
GZIPInputStream gzipInputStream = null;
GZIPInputStream getSize = null;
ByteArrayInputStream inputStream = null;
try {
if (request.getHeader("Content-Type").contains(MULTIPART_BOUNDARY)) {
//todo 采用的单个文件,可以改为多个
//MultipartFile multipartFile = req.getFile("file");
//这个是获取Gzip解压后文件的输入流
gzipInputStream = new GZIPInputStream(new BufferedInputStream(req.getFile("file").getInputStream()));
//用来计算这个解压后文件的大小
getSize = new GZIPInputStream(new BufferedInputStream(req.getFile("file").getInputStream()));
long size = 0;
//计算大小
while (getSize.available() > 0) {
byte[] buf = new byte[1024];
int read = getSize.read(buf);
if (read > 0) size += read;
}
//判断是不是dmp文件,是的话就以.dmp文件结尾上传
if (DMP_FILE_KEY.equals(request.getHeader(CONTENT_DISPOSITION))) {
minioClient.putObject(PutObjectArgs.builder().bucket(crashpadBucket).object(prefix + DMP).stream(gzipInputStream,size, -1).build());
}
//这个是获取我们其他的一些参数,name换成自己与前端约定的
String json = request.getParameter("name");
//将String 转换成 输入流
StringBuilder builder = new StringBuilder(json);
inputStream = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));
//上传
minioClient.putObject(PutObjectArgs.builder().bucket(crashpadBucket).object(prefix + JSONS).stream(inputStream,json.getBytes().length, -1).build());
return RestResponse.success("upload success ! ! !").setCode(0);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (InsufficientDataException e) {
e.printStackTrace();
} catch (InternalException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidResponseException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (ServerException e) {
e.printStackTrace();
} catch (XmlParserException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (gzipInputStream != null) {
gzipInputStream.close();
}
if (getSize != null) {
getSize.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return RestResponse.fail("upload fail ! ! !").setCode(1);
}
//创建文件前缀
private static String getObjectPrefix(String guid) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd-HH-mm-ss");
String dateStr = dateFormat.format(date);
UUID uuid = UUID.randomUUID();
return dateStr + "-" + guid + "-" + UUID.randomUUID().toString();
}
}
让我们来测试一下吧


查看结果

在啰嗦一下,如果有更好的方案,欢迎大家直接贴出来,呼我脸上,一起交流!^ _ ^
该博客介绍了如何在Java应用中接收并处理Gzip压缩文件,解压缩后将其内容上传到Minio存储服务。通过检查Content-Encoding头来识别Gzip文件,然后使用GZIPInputStream进行解压,计算文件大小,并根据文件类型决定上传路径。同时,博客作者还分享了上传JSON文件的代码,并提供了错误处理。最后,鼓励读者分享更好的解决方案。
5592

被折叠的 条评论
为什么被折叠?



