上传文件无名字处理

This commit is contained in:
开源oschina 2022-05-28 16:05:02 +08:00
parent 34f3a620e0
commit 31d07b9576
5 changed files with 52 additions and 16 deletions

View File

@ -155,7 +155,7 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.12</version>
<version>5.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency>

View File

@ -5,7 +5,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -17,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
@ -28,6 +26,7 @@ import com.fc.v2.model.auto.TsysUser;
import com.fc.v2.satoken.SaTokenUtil;
import com.fc.v2.service.SysFileService;
import com.fc.v2.util.SnowflakeIdWorker;
import cn.hutool.crypto.digest.MD5;
/**
* aws 对外提供服务端点
@ -102,20 +101,35 @@ public class OssEndpoint {
@PostMapping("/object/{bucketName}")
public AjaxResult createObject(@RequestBody MultipartFile object, @PathVariable String bucketName) throws Exception {
String fileName = object.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
String uuid=SnowflakeIdWorker.getUUID();
String fileSuffixName=uuid+suffixName;
String suffixName=".png";
String mediaKey="";
//文件名字
String fileSuffixName="";
if(fileName.lastIndexOf(".")!=-1) {//有后缀
suffixName = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
mediaKey=MD5.create().digestHex(fileSuffixName);
fileSuffixName=mediaKey+suffixName;
}else {//无后缀
//取得唯一id
mediaKey = MD5.create().digestHex(fileName+suffixName);
fileSuffixName=mediaKey+suffixName;
}
PutObjectResult putObjectResult=template.putObject(bucketName, fileSuffixName, object.getInputStream(), object.getSize(), object.getContentType());
if(putObjectResult!=null){
TsysUser tsysUser=SaTokenUtil.getUser();
SysFile sysFile=null;
if(tsysUser!=null) {
sysFile=new SysFile(uuid, fileSuffixName, bucketName, object.getSize(), object.getContentType(),SaTokenUtil.getUserId(), SaTokenUtil.getLoginName(), new Date(),null, null, null);
SysFile sysFile=sysFileService.selectByExamplefileName(fileSuffixName);
if(sysFile==null) {//等于空为新增查询不到的文件
if(SaTokenUtil.isLogin()) {
sysFile=new SysFile(SnowflakeIdWorker.getUUID(), fileSuffixName, bucketName, object.getSize(), object.getContentType(),SaTokenUtil.getUserId(), SaTokenUtil.getLoginName(), new Date(),null, null, null);
}else {
sysFile=new SysFile(SnowflakeIdWorker.getUUID(), fileSuffixName, bucketName, object.getSize(), object.getContentType(),"-", "-", new Date(),null, null, null);
}
int i=sysFileService.insertSelective(sysFile);
if(i>0){
return AjaxResult.successData(200,template.getObjectInfo(bucketName, fileSuffixName));
}
}else {
sysFile=new SysFile(uuid, fileSuffixName, bucketName, object.getSize(), object.getContentType(),"-", "-", new Date(),null, null, null);
}
int i=sysFileService.insertSelective(sysFile);
if(i>0){
return AjaxResult.successData(200,template.getObjectInfo(bucketName, fileSuffixName));
}
}

View File

@ -50,7 +50,17 @@ public class SaTokenUtil {
* @Date 2019年11月21日 上午9:58:26
*/
public static String getIp() {
return StpUtil.getTokenSession().getString("login_ip");
}
/**
* 判断是否登录
* @return
* @author fuce
* @Date 2019年11月21日 上午9:58:26
*/
public static boolean isLogin() {
return StpUtil.isLogin();
}
}

View File

@ -116,6 +116,18 @@ public class SysFileService implements BaseService<SysFile, SysFileExample> {
public List<SysFile> selectByExample(SysFileExample example) {
return sysFileMapper.selectByExample(example);
}
public SysFile selectByExamplefileName(String filename) {
SysFileExample example=new SysFileExample();
example.createCriteria().andFileNameEqualTo(filename);
List<SysFile> sysFiles=sysFileMapper.selectByExample(example);
if(sysFiles!=null&&sysFiles.size()>0) {
return sysFiles.get(0);
}
return null;
}
@Override
public long countByExample(SysFileExample example) {

View File

@ -27,8 +27,8 @@ fuce:
server :
port : 8080
##项目名字配置
#servlet :
# context-path : /demo
servlet :
context-path : /goview
tomcat :
uri-encoding : UTF-8
#xx 报错修改的地方