获取附件上传的文件
场景描述
在页面通过附件组件上传文件,在服务中获取上传的文件
实现思路
附件组件将文件上传至minio,从minio获取文件 minio的用户名和密码从minio的pods中获取
具体实现
1、设置依赖
2、在service目录下,新建MinioClientService.java文件,复制下面的代码到文件中
完整的MinioClientService.java文件中的代码
package main.service;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.xmlpull.v1.XmlPullParserException;
import com.justep.util.ContextUtil;
import com.justep.util.StringUtils;
import com.justep.util.SpringWebUtil;
import io.minio.MinioClient;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidArgumentException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.minio.errors.NoResponseException;
@Service
public class MinioClientService {
private static final Logger logger = LoggerFactory.getLogger(MinioClientService.class);
private MinioClient minioClient;
public MinioClient getMinioClient() {
if (minioClient == null) {
try {
String minioServiceAddress = ContextUtil.getEnv("MAIN_MINIO_SERVICEADDRESS");
if (StringUtils.isEmpty(minioServiceAddress)) {
minioServiceAddress = ContextUtil.getEnv("MINIO_SERVICEADDRESS");
}
String minioPort = ContextUtil.getEnv("MAIN_MINIO_PORT");
if (StringUtils.isEmpty(minioPort)) {
minioPort = ContextUtil.getEnv("MINIO_PORT");
}
String minioUserName = ContextUtil.getEnv("MAIN_MINIO_USERNAME");
if (StringUtils.isEmpty(minioUserName)) {
minioUserName = "accessKey";
}
String minioPassword = ContextUtil.getEnv("MAIN_MINIO_PASSOWRD");
if (StringUtils.isEmpty(minioPassword)) {
minioPassword = "secretKey";
}
minioClient = new MinioClient("http://" + minioServiceAddress + ":" + minioPort, minioUserName,
minioPassword);
} catch (InvalidEndpointException | InvalidPortException e) {
throw new RuntimeException(e.getMessage()+"", e);
}
}
return minioClient;
}
/**
* 获取InputStream数据
* @param bucketName
* @param objectName
* @return
*/
public InputStream getObject(String bucketName, String objectName) {
try {
InputStream in = getMinioClient().getObject(bucketName, objectName);
return in;
} catch (InvalidKeyException | InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException
| NoResponseException | ErrorResponseException | InternalException | InvalidArgumentException
| IOException | XmlPullParserException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage()+"", e);
}
}
/**
* 保存数据到MinIO
* @param bucketName
* @param objectName
* @param stream
* @param size
* @param contentType
*/
public void putObject(String bucketName, String objectName, InputStream stream, long size, String contentType) {
try {
getMinioClient().putObject(bucketName, objectName, stream, size, contentType);
} catch (InvalidKeyException | InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException
| NoResponseException | ErrorResponseException | InternalException | InvalidArgumentException
| IOException | XmlPullParserException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage()+"", e);
}
}
public String getBucketName() {
HttpServletRequest request = SpringWebUtil.getRequest();
String bucketName = request.getHeader("X-Tenant-Key");
return bucketName;
}
}
3、在service文件中,中引入MinioClientService
@Autowired
private MinioClientService minioClientService;
4、在service文件中,获取附件字段内容,调用minioClientService的getObject方法获取文件流
OrderM orderM = orderMMapper.findByPrimaryKey(rowid);
String fid = orderM.getFid();
String attachmentFile = orderM.getAttachment();//获取附件字段内容
if (StringUtils.isEmpty(attachmentFile)) {
return null;
}
String bucketName = minioClientService.getBucketName();
JSONArray jsonArray = JSON.parseArray(attachmentFile);//附件字段内容是JSON数组
String dirName = ContextUtil.getEnv("JUSTEP_HOME")+"/tmp/attachment/"+fid;//存储文件的目录
for (int i=0; i<jsonArray.size(); i++) {
JSONObject json = jsonArray.getJSONObject(i);//获取一个文件的JSON内容
InputStream is = minioClientService.getObject(bucketName, json.getString("storeFileName"));//获取InputStream流
String fileName = dirName+"/"+json.getString("realFileName");//存储文件的名称
writeFile(is, fileName);//写入文件
}
附上写入文件writeFile方法的代码
public static void writeFile(InputStream in, String filePath) {
FileOutputStream fos = null;
try {
File file = new File(filePath);
File dir = file.getParentFile();
if (!dir.isDirectory()) {
dir.mkdirs();
}
fos = new FileOutputStream(file);
IOUtils.copy(in, fos);
} catch (Exception e) {
throw new RuntimeException(e.getMessage() + "", e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
}
}
}
}