Commit 60f77b3f by Your Name Committed by chenzg

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

parent 631d9355
package cn.timer.api.service;
import cn.timer.api.service.impl.OSSServiceImpl;
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import cn.timer.api.utils.aliyun.OSSUtil;
import cn.timer.api.utils.random.RandomNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* 上传文件的实现层
*/
@Service
public class OSSService implements OSSServiceImpl {
@Autowired
private OSSUtil oss;
/**
*
* @param orgCode
* @param moudle
* @param file
* @return
*/
@Override
public Result<String> upload(int orgCode, String moudle, MultipartFile file) {
String url = null;
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());
} catch (IOException e) {
e.printStackTrace();
}
return ResultUtil.data(url, "上传成功!");
}
}
public static File filePathUpload(int orgCode,String moudle,String randomNickname,String originalFilename){
String path = "8timer2.0/" + orgCode + "/" + moudle + "/" + randomNickname + "/" + originalFilename;
File filePath = new File(path);
return filePath;
}
}
package cn.timer.api.service.impl;
import cn.timer.api.utils.Result;
import org.springframework.web.multipart.MultipartFile;
public interface OSSServiceImpl {
public Result<String> upload(int orgCode, String moudle, MultipartFile file);
}
......@@ -101,7 +101,7 @@ public class OSSUtil {
* @param ins 输入流
* @return
*/
public String uploadFile(String path, InputStream ins) {
public String uploadFile(File path, InputStream ins) {
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
......@@ -112,7 +112,7 @@ public class OSSUtil {
// 创建PutObjectRequest对象。
// 项目名+用户公司id+模块名+文件夹名+文件名
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, ins);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path.getPath(), ins);
// [file1,file2,file3],["mk1/img","mk1"]
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
......@@ -129,7 +129,7 @@ public class OSSUtil {
Date now = new Date();
Date expiration = DateUtil.offset(now, DateField.YEAR, 100); // 一百年之后过期
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(bucketName, path, expiration);
URL url = ossClient.generatePresignedUrl(bucketName, path.getPath(), expiration);
Logoutput(String.valueOf(url));
// 关闭OSSClient。
......@@ -180,7 +180,6 @@ public class OSSUtil {
/**
* 4.从OSS下载文件
*
* @param objectName 完整路径的文件名
* @param path 本地储存文件路径
* @param orgCode 公司id
* @param dir 包名
......@@ -236,7 +235,7 @@ public class OSSUtil {
/**
* 文件输入流批量上传
*
* @param userBean 获取参数
* @param request 获取参数
* @param files 批量文件
* @return 返回上传成功或失败的信息
*/
......
package cn.timer.api.utils.random;
import java.util.Random;
public class RandomNum {
/**
* java生成随机数字和字母组合10位数
* @return
*/
public static String getRandomNickname() {
String val = "";
Random random = new Random();
for (int i = 0; i < 10; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equalsIgnoreCase(charOrNum)) {
// 取得大写字母还是小写字母
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextInt(26));
} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
}
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