Commit df105773 by Your Name

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

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