Commit df105773 by Your Name

更新了上传文件的出现的问题

parent e5fd486e
......@@ -421,6 +421,10 @@
<artifactId>htmlunit</artifactId>
<version>2.43.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies>
<repositories>
......
......@@ -3,17 +3,12 @@ package cn.timer.api.controller.oss;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import cn.timer.api.service.OSSService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import cn.timer.api.config.annotation.CurrentUser;
......@@ -32,6 +27,8 @@ import io.swagger.annotations.ApiOperation;
public class OSSController {
@Autowired
private OSSUtil oss;
@Autowired
private OSSService ossService;
/**
* 上传普通文件
......@@ -41,21 +38,11 @@ public class OSSController {
*/
@PostMapping(value = "/upload")
@ApiOperation(value = "上传普通文件", httpMethod = "POST", notes = "接口发布说明")
public Result<String> upload(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,
@Param("file") MultipartFile file) {
String url = null;
int uuid = (int) (Math.random()*100+1);
String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" +uuid+"-"+ file.getOriginalFilename();
if (file == null || file.getSize() <= 0) {
return ResultUtil.error("上传的文件为空,请重新选择!");
} else {
try {
url = oss.uploadFile(path, file.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return ResultUtil.data(url, "上传成功!");
}
@ResponseBody
public Result<String> upload(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,@RequestParam("filename") MultipartFile file) throws Exception {
Result<String> data = ossService.upload(userBean.getOrgCode(), moudle, file);
return data;
}
/**
......@@ -68,23 +55,10 @@ public class OSSController {
@ApiOperation(value = "批量上传普通文件", httpMethod = "POST", notes = "接口发布说明")
public Result<Object> uploads(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,
@Param("files") List<MultipartFile> files) {
String url = null;
List<String> list = new ArrayList<String>();
for (MultipartFile file : files) {
int uuid = (int) (Math.random()*100+1);
String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" +uuid+"-"+ file.getOriginalFilename();
if (file == null || file.getSize() <= 0) {
return ResultUtil.error("上传的文件为空,请重新选择!");
} else {
try {
url = oss.uploadFile(path, file.getInputStream());
list.add(url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ResultUtil.data(list, "上传成功!");
Result<Object> data = ossService.uploads(userBean.getOrgCode(), moudle, files);
return data;
}
/**
......@@ -94,8 +68,7 @@ public class OSSController {
@ApiOperation(value = "上传私密文件", httpMethod = "POST", notes = "接口发布说明")
public Result<String> uploadPrivate(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,
@Param("file") MultipartFile file) {
int uuid = (int) (Math.random()*100+1);
String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" +uuid+"-"+ file.getOriginalFilename();
String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/"+ file.getOriginalFilename();
if (file == null || file.getSize() <= 0) {
return ResultUtil.error("上传的文件为空,请重新选择!");
} else {
......
......@@ -11,6 +11,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 上传文件的实现层
......@@ -54,4 +56,28 @@ public class OSSService implements OSSServiceImpl {
File filePath = new File(path);
return filePath;
}
public Result<Object> uploads(int orgCode, String moudle, List<MultipartFile> files){
String url = null;
List<String> list = new ArrayList<String>();
for (MultipartFile file : files) {
String randomNickname = RandomNum.getRandomNickname();
File filePath = filePathUpload(orgCode, moudle, randomNickname, file.getOriginalFilename());
int i = 0;
while (filePath.exists()) {
filePath = filePathUpload(orgCode,moudle,randomNickname,file.getOriginalFilename());
i++;
}
if (file == null || file.getSize() <= 0) {
return ResultUtil.error("上传的文件为空,请重新选择!");
} else {
try {
url = oss.uploadFile(filePath, file.getInputStream());
list.add(url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ResultUtil.data(list, "上传成功!");
}
}
......@@ -3,7 +3,10 @@ package cn.timer.api.service.impl;
import cn.timer.api.utils.Result;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface OSSServiceImpl {
public Result<String> upload(int orgCode, String moudle, MultipartFile file);
public Result<Object> uploads(int orgCode, String moudle, List<MultipartFile> files);
}
......@@ -61,7 +61,7 @@ public class AliyunSMS {
/**
*
* @param phone
* @param code
* @param templateCode 1."身份验证验证码" 2."登录确认验证码" 3."登录异常验证码" 4."用户注册验证码"
* 5."修改密码验证码" 6."信息变更验证码"
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment