Java file uploading and downloading - use Minio

Minio template class:

@RequiredArgsConstructor
public class MinioTemplate implements InitializingBean {
private final String endpoint;
private final String accessKey;
private final String secretKey;
private MinioClient client;

/**
* 创建bucket
*
* @param bucketName bucket名称
*/
@SneakyThrows
public void createBucket(String bucketName) {
if (!client.bucketExists(bucketName)) {
client.makeBucket(bucketName);
}
}

/**
* 获取全部bucket
* <p>
* https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
*/
@SneakyThrows
List public <Bucket> getAllBuckets () {
return client.listBuckets ();
}

/ **
* The information acquired bucketName
* @param bucketName bucket name
* /
@SneakyThrows
public Optional The <Bucket> getBucket (String bucketName) {
return client.listBuckets .. () .stream () filter (. B -> b.name () the equals (bucketName)) the findFirst ();
}

/ **
* The deletion information bucketName
* @param bucketName bucket name
* /
@SneakyThrows
public void removeBucket (String bucketName) {
client.removeBucket (bucketName);
}

/ **
* query file according to the file pre
*
* @param bucketName bucket name
* @param prefix 前缀
* @param recursive 是否递归查询
* @return MinioItem 列表
*/
@SneakyThrows
public List<MinioItem> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
List<MinioItem> objectList = new ArrayList<>();
Iterable<Result<Item>> objectsIterator = client
.listObjects(bucketName, prefix, recursive);

while (objectsIterator.iterator().hasNext()) {
objectList.add(new MinioItem(objectsIterator.iterator().next().get()));
}
return objectList;
}

/**
* 获取文件外链
*
* @param bucketName bucket名称
* @Param objectName file name
* @param expires expiration time <=. 7
* @return URL
* /
@SneakyThrows
public getObjectURL String (String bucketName, objectName String, Integer Expires) {
return client.presignedGetObject (bucketName, objectName, Expires);
}

/ **
* Get file
*
* @param bucketName bucket name
* @param objectName file name
* @return binary stream
* /
@SneakyThrows
public the InputStream the getObject (bucketName String, String objectName) {
return client.getObject (bucketName, objectName);
}

/ **
* Upload file
*
* @Param bucketName bucket Name
* @param objectName file name
* @param stream stream file
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
* /
public void putObject (bucketName String, String objectName, the InputStream Stream) throws Exception {
client.putObject (bucketName, objectName, Stream, stream.available (), "file application / OCTET-Stream");
}

/ **
* upload
*
* @param bucketName bucket name
* @param objectName file name
* @param stream file stream
* @param size size
* @param contextType type
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
client.putObject(bucketName, objectName, stream, size, contextType);
}

/**
* 获取文件信息
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
*/
public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
return client.statObject(bucketName, objectName);
}

/**
* Delete files
*
* @param bucketName bucket Name
* @param objectName file name
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
* /
public void removeObject (String bucketName, String objectName) throws Exception {
client.removeObject (bucketName, objectName);
}

@Override
public void afterPropertiesSet () throws Exception {
Assert.hasText (Endpoint, "empty Minio URL");
Assert.hasText (the accessKey, "Minio accessKey empty ");
Assert.hasText (secretKey," Minio secretKey empty ");
this.client = new new MinioClient (Endpoint, accessKey, secretKey);
}


file Upload method:
public String upload(@RequestParam("file") MultipartFile file) {
String fileName = file.getOriginalFilename();
Map<String, String> resultMap = new HashMap<>(4);
resultMap.put("bucketName", "bucketName");
    resultMap.put("fileName", fileName);
try {
minioTemplate.putObject("bucketName", fileName, file.getInputStream());

  } catch (Exception e) {
        return "Upload Failed"; 
}
return "Upload successful";
} file download method:


public void download(String fileName, HttpServletResponse response, HttpServletRequest request) {
String userAgent = request.getHeader("User-Agent");
String[] nameArray = StrUtil.split(fileName, "-");
try (InputStream inputStream = minioTemplate.getObject(nameArray[0], nameArray[1])) {
//解决乱码
if ( //IE 8 至 IE 10
userAgent.toUpperCase().contains("MSIE") ||
//IE 11
userAgent.contains("Trident/7.0")) {
fileName = java.net.URLEncoder.encode(nameArray[1], "UTF-8");
} else{
fileName = new String(nameArray[1].getBytes("UTF-8"),"iso-8859-1");
}
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
IoUtil.copy(inputStream, response.getOutputStream());
} catch (Exception e) {
log.error("文件读取异常", e);
}
}


Guess you like

Origin www.cnblogs.com/youyouxiaosheng-lh/p/11026877.html