上传文件到IOSS里面
@Controller
@RequestMapping("ossFile")
public class OssFile1Controller {
@RequestMapping(value = "/upload",produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
@ResponseBody
public ResultUtil upload(MultipartFile file, HttpServletRequest request) {
if (!file.isEmpty()) {
String fileSub = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
Random d = new Random();
String img = UUID.randomUUID().toString().replace("-", "") + d.nextInt(10000) + "" + fileSub;
try {
// 生成文件名称
String nameSuffix ="文件的路径"+img; //上传原始图片到阿里云
OssFileUtil ossFileUtil = new OssFileUtil();
String uploadPath = ossFileUtil.uploadAliyun(file,nameSuffix);
File f=new File(GlobalUtil.getValue(uploadPath));
if(!f.exists()){
f.mkdirs();
}
file.transferTo(new File(f, uploadPath));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Map<String, String> map = new HashMap<>();
map.put("src", img);
return ResultUtil.ok(map);
} else {
return ResultUtil.error("文件格式不支持,请重新选择!");
}
}
pom上面添加
<!-- 阿里云oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>