🐛 fixed uploadService

error token
This commit is contained in:
毛毛虫 2022-01-14 11:55:15 +08:00
parent 49a1f6d021
commit 487d436b0c
7 changed files with 104 additions and 66 deletions

View File

@ -23,6 +23,17 @@ public class ForestFile {
@Column(name = "id")
private Long id;
/**
* 文件大小
*/
@Column(name = "file_size")
private long fileSize;
/**
* 文件类型-文件后缀
*/
@Column(name = "file_type")
private String fileType;
/**
* 访问路径
*/

View File

@ -15,9 +15,10 @@ public interface ForestFileMapper extends Mapper<ForestFile> {
*
* @param md5Value md5值
* @param createdBy 创建人
* @return
* @param createdBy 创建人
* @param fileType 文件类型
*/
String getFileUrlByMd5(@Param("md5Value") String md5Value, @Param("createdBy") long createdBy);
String getFileUrlByMd5(@Param("md5Value") String md5Value, @Param("createdBy") long createdBy, @Param("fileType") String fileType);
/**
* 插入文件对象
@ -26,8 +27,10 @@ public interface ForestFileMapper extends Mapper<ForestFile> {
* @param filePath 上传路径
* @param md5Value md5值
* @param createdBy 创建人
* @param fileSize 文件大小
* @param fileType 文件类型
* @return
*/
int insertForestFile(@Param("fileUrl") String fileUrl, @Param("filePath") String filePath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy);
int insertForestFile(@Param("fileUrl") String fileUrl, @Param("filePath") String filePath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy, @Param("fileSize") long fileSize, @Param("fileType") String fileType);
}

View File

@ -17,9 +17,10 @@ public interface ForestFileService extends Service<ForestFile> {
*
* @param md5Value md5值
* @param createdBy 用户id
* @param fileType 文件类型
* @return
*/
String getFileUrlByMd5(String md5Value, long createdBy);
String getFileUrlByMd5(String md5Value, long createdBy, String fileType);
/**
* 插入文件对象
@ -28,7 +29,9 @@ public interface ForestFileService extends Service<ForestFile> {
* @param filePath 上传路径
* @param md5Value md5值
* @param createdBy 创建人
* @param fileSize 文件大小
* @param fileType 文件类型
* @return
*/
int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy);
int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy, long fileSize, String fileType);
}

View File

@ -24,11 +24,12 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
*
* @param md5Value md5值
* @param createdBy 用户id
* @param fileType 文件类型
* @return
*/
@Override
public String getFileUrlByMd5(String md5Value, long createdBy) {
return forestFileMapper.getFileUrlByMd5(md5Value, createdBy);
public String getFileUrlByMd5(String md5Value, long createdBy, String fileType) {
return forestFileMapper.getFileUrlByMd5(md5Value, createdBy, fileType);
}
/**
@ -38,10 +39,12 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
* @param filePath 上传路径
* @param md5Value md5值
* @param createdBy 创建人
* @param fileSize 文件大小
* @param fileType 文件类型
* @return
*/
@Override
public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy) {
return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy);
public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy, long fileSize, String fileType) {
return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy, fileSize, fileType);
}
}

View File

@ -18,7 +18,7 @@ public class FileUtils {
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
/**
* 获取文件扩展名
* 获取文件扩展名,.
*
* @param filename
* @return
@ -31,6 +31,7 @@ public class FileUtils {
* 获取文件扩展名
*
* @param filename
* @param defExt 默认文件后缀名 .
* @return
*/
public static String getExtend(String filename, String defExt) {
@ -41,7 +42,7 @@ public class FileUtils {
int i = filename.lastIndexOf('.');
if ((i > 0) && (i < (filename.length() - 1))) {
String result = filename.substring(i + 1).toLowerCase();
String result = filename.substring(i).toLowerCase();
if (result.contains("?")) {
return result.split("\\?")[0];
}

View File

@ -42,8 +42,7 @@ public class UploadController {
private final static String UPLOAD_URL = "/api/upload/file/batch";
private final static String LINK_TO_IMAGE_URL = "/api/upload/file/link";
private static Environment env = SpringContextHolder.getBean(Environment.class);
private static final Environment env = SpringContextHolder.getBean(Environment.class);
@Resource
private ForestFileService forestFileService;
@ -120,8 +119,15 @@ public class UploadController {
}
TokenUser tokenUser = getTokenUser(request);
Map data = new HashMap(2);
if (multipartFile.getSize() == 0) {
data.put("message", "上传失败!");
return GlobalResultGenerator.genSuccessResult(data);
}
String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser());
String orgName = multipartFile.getOriginalFilename();
String fileType = FileUtils.getExtend(orgName);
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
if (StringUtils.isNotEmpty(fileUrl)) {
data.put("url", fileUrl);
return GlobalResultGenerator.genSuccessResult(data);
@ -137,15 +143,15 @@ public class UploadController {
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
String orgName = multipartFile.getOriginalFilename();
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
String fileName = System.currentTimeMillis() + fileType;
String savePath = file.getPath() + File.separator + fileName;
fileUrl = localPath + fileName;
File saveFile = new File(savePath);
try {
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
data.put("url", fileUrl);
} catch (IOException e) {
data.put("message", "上传失败!");
@ -174,21 +180,25 @@ public class UploadController {
for (int i = 0, len = multipartFiles.length; i < len; i++) {
MultipartFile multipartFile = multipartFiles[i];
String orgName = multipartFile.getOriginalFilename();
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
if (multipartFile.getSize() == 0) {
errFiles.add(orgName);
continue;
}
String fileType = FileUtils.getExtend(orgName);
String fileName = System.currentTimeMillis() + fileType;
String savePath = file.getPath() + File.separator + fileName;
File saveFile = new File(savePath);
try (InputStream in = multipartFiles[i].getInputStream();
OutputStream out = Files.newOutputStream(saveFile.toPath())) {
try (InputStream in = multipartFiles[i].getInputStream(); OutputStream out = Files.newOutputStream(saveFile.toPath())) {
String md5 = DigestUtils.md5DigestAsHex(in);
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser());
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
if (StringUtils.isNotEmpty(fileUrl)) {
successMap.put(orgName, fileUrl);
continue;
}
fileUrl = localPath + fileName;
FileCopyUtils.copy(in, out);
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
successMap.put(orgName, localPath + fileName);
} catch (IOException e) {
errFiles.add(orgName);
@ -246,53 +256,57 @@ public class UploadController {
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36");
conn.setRequestProperty("referer", "");
//得到输入流
InputStream inputStream = conn.getInputStream();
// 获取文件md5值
String md5 = DigestUtils.md5DigestAsHex(inputStream);
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser());
Map data = new HashMap(2);
data.put("originalURL", url);
//得到输入流
try (InputStream inputStream = conn.getInputStream()) {
//获取自己数组
byte[] getData = readInputStream(inputStream);
if (getData.length == 0) {
data.put("message", "文件为空!");
return GlobalResultGenerator.genSuccessResult(data);
}
if (StringUtils.isNotEmpty(fileUrl)) {
// 获取文件md5值
String md5 = DigestUtils.md5DigestAsHex(inputStream);
String fileType = FileUtils.getExtend(url);
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
data.put("originalURL", url);
if (StringUtils.isNotEmpty(fileUrl)) {
data.put("url", fileUrl);
return GlobalResultGenerator.genSuccessResult(data);
}
Integer type = linkToImageUrlDTO.getType();
if (Objects.isNull(type)) {
type = 1;
}
String typePath = getTypePath(type);
//图片存储路径
String ctxHeadPicPath = env.getProperty("resource.pic-path");
String dir = ctxHeadPicPath + "/" + typePath;
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
String fileName = System.currentTimeMillis() + fileType;
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
String savePath = file.getPath() + File.separator + fileName;
File saveFile = new File(savePath);
FileCopyUtils.copy(getData, saveFile);
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), getData.length, fileType);
data.put("originalURL", url);
data.put("url", fileUrl);
return GlobalResultGenerator.genSuccessResult(data);
} catch (IOException e) {
data.put("message", "上传失败");
return GlobalResultGenerator.genSuccessResult(data);
}
Integer type = linkToImageUrlDTO.getType();
if (Objects.isNull(type)) {
type = 1;
}
String typePath = getTypePath(type);
//图片存储路径
String ctxHeadPicPath = env.getProperty("resource.pic-path");
String dir = ctxHeadPicPath + "/" + typePath;
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url);
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
String savePath = file.getPath() + File.separator + fileName;
File saveFile = new File(savePath);
try {
//获取自己数组
byte[] getData = readInputStream(inputStream);
FileCopyUtils.copy(getData, saveFile);
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
data.put("originalURL", url);
data.put("url", fileUrl);
} catch (IOException e) {
data.put("message", "上传失败!");
}
return GlobalResultGenerator.genSuccessResult(data);
}

View File

@ -3,6 +3,8 @@
<mapper namespace="com.rymcu.forest.mapper.ForestFileMapper">
<resultMap id="BaseResultMap" type="com.rymcu.forest.entity.ForestFile">
<id column="id" property="id"/>
<id column="file_size" property="fileSize"/>
<id column="file_type" property="fileType"/>
<result column="md5_value" property="md5Value"/>
<result column="file_path" property="filePath"/>
<result column="file_url" property="fileUrl"/>
@ -12,8 +14,8 @@
</resultMap>
<insert id="insertForestFile">
insert into forest_file (md5_value, file_path, file_url, created_time, created_by)
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy})
insert into forest_file (md5_value, file_path, file_url, created_time, created_by, file_size, file_type)
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}, #{fileSize}, #{fileType})
</insert>
<select id="getFileUrlByMd5" resultType="java.lang.String">
@ -21,6 +23,7 @@
from forest_file
where md5_value = #{md5Value}
and created_by = #{createdBy}
and file_type = #{fileType}
</select>
</mapper>