上传文件无名字处理

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> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>4.1.12</version> <version>5.3.3</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity --> <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency> <dependency>

View File

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

View File

@ -50,7 +50,17 @@ public class SaTokenUtil {
* @Date 2019年11月21日 上午9:58:26 * @Date 2019年11月21日 上午9:58:26
*/ */
public static String getIp() { public static String getIp() {
return StpUtil.getTokenSession().getString("login_ip"); 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

@ -117,6 +117,18 @@ public class SysFileService implements BaseService<SysFile, SysFileExample> {
return sysFileMapper.selectByExample(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 @Override
public long countByExample(SysFileExample example) { public long countByExample(SysFileExample example) {
return sysFileMapper.countByExample(example); return sysFileMapper.countByExample(example);

View File

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