本帖最后由 wushaominkk 于 2019-7-12 14:13 编辑
不多说,直接上代码:
这是我创建的OSS工具类:
[Java] 纯文本查看 复制代码 @Component
public class OssUtils {
private static String [i]accessKeyId[/i];
private static String [i]accessKeySecret[/i];
private static String [i]bucketName[/i];
//外网
private static String [i]bucketOuter[/i];
//内网
private static String [i]bucketIntranet[/i];
private static String [i]endpoint[/i];
public static String [i]ossdir[/i];
private static OSSClient [i]ossClientStatic[/i];
public OssUtils(@Value("${oss.accessKeyId}") String accessKeyId,
@Value("${oss.accessKeySecret}") String accessKeySecret,
@Value("${oss.bucketName}") String bucketName,
@Value("${oss.bucketOuter}") String bucketOuter,
@Value("${oss.bucketIntranet}")String bucketIntranet,
@Value("${oss.endpoint}") String endpoint,
@Value("${oss.ossdir}") String ossdir ) {
this.[i]accessKeyId [/i]= accessKeyId;
this.[i]accessKeySecret[/i]=accessKeySecret;
this.[i]bucketName[/i]=bucketName;
this.[i]bucketOuter[/i]=bucketOuter;
this.[i]bucketIntranet[/i]=bucketIntranet;
this.[i]endpoint[/i]=endpoint;
this.[i]ossdir[/i]=ossdir;
[i]ossClientStatic [/i]= new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
[i]/**
[/i][i] * [/i][i]上传到[/i][i]OSS[/i][i]服务器 如果同名文件会覆盖服务器上的
[/i][i] [/i][i]*
[/i][i] * [/i][i]@Param [/i][i]fileName [/i][i]文件名称 包括后缀名
[/i][i] [/i][i]* [/i][i]@param [/i][i]instream [/i][i]文件流
[/i][i] [/i][i]* [/i][i]@Return [/i][i]出错返回[/i][i]"" ,[/i][i]唯一[/i][i]MD5[/i][i]数字签名
[/i][i] [/i][i]*/
[/i][i] [/i]public static String upload(String fileName, InputStream instream) {
String resultStr = "";
try {
// 创建上传Object的Metadata
ObjectMetadata objectMetadata = new ObjectMetadata();
// 上传文件 (上传文件流的形式)
PutObjectResult putResult = [i]ossClientStatic[/i].putObject([i]bucketName[/i], [i]ossdir[/i]+fileName, instream, objectMetadata);
// 解析结果
resultStr = putResult.getETag();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (instream != null) {
instream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultStr;
}
}
下面是上传文件的接口
[i]/**
[/i][i] * [/i][i]文件上传接口
[/i][i] [/i][i]* [/i][i]@param [/i][i]request
[/i][i] [/i][i]* [/i][i]@return
[/i][i] [/i][i]*/
[/i]@PostMapping("/upload")
@ApiOperation(value = "上传文件", httpMethod = "POST", notes = "上传文件")
public JsonResult upload(@ApiParam(value="上传文件",required=true)HttpServletRequest request) {
try {
logger.info("**************上传文件开始 : [{}]::",request);
long startTime = System.[i]currentTimeMillis[/i]();
List<String> files = new ArrayList<>();
logger.info("************** 创建解析器 *****************" );
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
logger.info("************** 上传的文件内容 isMultipart*****************::[{}]::", resolver.isMultipart(request));
if (resolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
String path = StringUtils.[i]appendIfMissing[/i](StringUtils.[i]stripEnd[/i](StringUtils.[i]trimToEmpty[/i](request.getParameter("path")), "/"), "/");
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next().toString());
if (file != null) {
InputStream inputStream = file.getInputStream();
String originalName = file.getOriginalFilename();
String extension = originalName.substring(originalName.lastIndexOf('.'));
String fileName = IdUtil.[i]uuid[/i]() + extension;
OssUtils.[i]upload[/i](fileName,inputStream);
String url =OssUtils.[i]ossdir[/i]+fileName;
files.add(url);
}
}
}
long endTime = System.[i]currentTimeMillis[/i]();
logger.debug("**************文件上传耗时:{} ms", endTime - startTime);
return JsonResult.[i]OK[/i](files);
} catch (BuzEx e) {
logger.error("**************上传时发生异常***********::[{}]::", e.getMessage());
return new JsonResult(e.getCode().getCode(), null, e.getCode().getMessage());
} catch(Exception e) {
logger.error("**************上传时发生异常***********::[{}]::", e.getMessage());
return JsonResult.[i]build[/i](BuzCode.[i]C_FILE_ERROR[/i]);
}
} |