记得上下班打卡 | git大法好,push需谨慎

Commit 1120d730 authored by 胡佳晨's avatar 胡佳晨

提交 platform

parent 3f399163
...@@ -5,6 +5,8 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -5,6 +5,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest; import java.security.MessageDigest;
/** /**
...@@ -98,4 +100,28 @@ public class FilesUtils { ...@@ -98,4 +100,28 @@ public class FilesUtils {
} }
} }
public static File inputStreamToFile(String url, String name){
try {
HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
httpUrl.connect();
InputStream ins = httpUrl.getInputStream();
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
} }
...@@ -136,4 +136,62 @@ public class AlOssController { ...@@ -136,4 +136,62 @@ public class AlOssController {
return ResponseDto.success(uploadVo); return ResponseDto.success(uploadVo);
} }
} @PostMapping("/uploadUrl")
@ApiOperation("阿里云上传URL")
@ApiImplicitParams({
@ApiImplicitParam(type = "form", dataType = "String", name = "pathName", value = "归类的文件夹名称 比如banner上传就传 banner 返回的地址就会是bnanner/XXX", defaultValue = "other"),
@ApiImplicitParam(type = "form", dataType = "String", name = "picUrl", value = "picUrl", required = false),
})
public ResponseDto<UploadVo> upload(
@RequestParam(defaultValue = "0", required = false) String pathName,
@RequestParam(defaultValue = "0", required = false) String picUrl
) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
File fileNew = FilesUtils.inputStreamToFile("http://mmbiz.qpic.cn/mmbiz_jpg/Ek5ncx8aHajn48x4aaqcNaIS5p7EJsMJaD7OtXdh8pFqY1GPuPSaMiaNSCL3YJHnOfERmOVy54zOKkjs5vtxjww/0", System.currentTimeMillis() + ".png");
// file md5
String fileMD5 = FilesUtils.getFileMD5(fileNew);
List<PlatformOssFiles> platformOssFiles = platformOssFilesMapper.selectList(
Wrappers.lambdaQuery(PlatformOssFiles.class)
.eq(PlatformOssFiles::getMd5str, fileMD5)
);
UploadVo uploadVo = new UploadVo();
if (CollectionUtils.isEmpty(platformOssFiles)) {
String filename ;
if(pathName.contains(".")){
filename = pathName;
}else{
filename = "other.jpeg";
}
//这里文件名用了uuid 防止重复,可以根据自己的需要来写
String uploadName = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));// 078a77e0-cf80-481b-824c-5935247cff15.jpeg
uploadName = uploadName.replace("-", "");// 078a77e0cf80481b824c5935247cff15.jpeg
String uploadpath = pathName + "/" + DateUtil.format(LocalDateTime.now(), DateUtil.Formatter.yyyy_MM_dd2) + "/" + uploadName;
String buckName = "img-zhengzai-tv";
PutObjectRequest putObjectRequest = new PutObjectRequest(buckName, uploadpath, fileNew);
ossClient.putObject(putObjectRequest);
ossClient.shutdown();
// 入库
PlatformOssFiles platformOssFilesDate = new PlatformOssFiles();
String ossFilesId = IDGenerator.nextSnowId();
platformOssFilesDate.setOssFilesId(ossFilesId);
platformOssFilesDate.setOssPath(uploadpath);
platformOssFilesDate.setFileName(filename);
platformOssFilesDate.setMd5str(fileMD5);
platformOssFilesDate.setCreatedAt(DateUtil.getNowTime());
platformOssFilesMapper.insert(platformOssFilesDate);
BeanUtils.copyProperties(platformOssFilesDate, uploadVo);
} else {
PlatformOssFiles platformOssFilesOne = platformOssFiles.get(0);
BeanUtils.copyProperties(platformOssFilesOne, uploadVo);
}
FilesUtils.delteTempFile(fileNew); // 删除临时文件
return ResponseDto.success(uploadVo);
}
}
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