文件存储服务

This commit is contained in:
jiang 2024-07-05 10:35:30 +08:00
parent d5acac245a
commit bbf47b7043
2 changed files with 44 additions and 78 deletions

View File

@ -12,8 +12,10 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import com.bonus.common.core.utils.StringUtils;
import org.springframework.web.multipart.MultipartFile;
@ -24,12 +26,15 @@ import org.springframework.web.multipart.MultipartFile;
* @author bonus
*/
public class FileUtils
{
/** 字符常量:斜杠 {@code '/'} */
public class FileUtils {
/**
* 字符常量斜杠 {@code '/'}
*/
public static final char SLASH = '/';
/** 字符常量:反斜杠 {@code '\\'} */
/**
* 字符常量反斜杠 {@code '\\'}
*/
public static final char BACKSLASH = '\\';
public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
@ -40,49 +45,33 @@ public class FileUtils
* @param filePath 文件路径
* @param os 输出流
*/
public static void writeBytes(String filePath, OutputStream os) throws IOException
{
public static void writeBytes(String filePath, OutputStream os) throws IOException {
FileInputStream fis = null;
try
{
try {
File file = new File(filePath);
if (!file.exists())
{
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int length;
while ((length = fis.read(b)) > 0)
{
while ((length = fis.read(b)) > 0) {
os.write(b, 0, length);
}
}
catch (IOException e)
{
} catch (IOException e) {
throw e;
}
finally
{
if (os != null)
{
try
{
} finally {
if (os != null) {
try {
os.close();
}
catch (IOException e1)
{
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fis != null)
{
try
{
if (fis != null) {
try {
fis.close();
}
catch (IOException e1)
{
} catch (IOException e1) {
e1.printStackTrace();
}
}
@ -95,13 +84,11 @@ public class FileUtils
* @param filePath 文件
* @return
*/
public static boolean deleteFile(String filePath)
{
public static boolean deleteFile(String filePath) {
boolean flag = false;
File file = new File(filePath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists())
{
if (file.isFile() && file.exists()) {
flag = file.delete();
}
return flag;
@ -113,8 +100,7 @@ public class FileUtils
* @param filename 文件名称
* @return true 正常 false 非法
*/
public static boolean isValidFilename(String filename)
{
public static boolean isValidFilename(String filename) {
return filename.matches(FILENAME_PATTERN);
}
@ -124,11 +110,9 @@ public class FileUtils
* @param resource 需要下载的文件
* @return true 正常 false 非法
*/
public static boolean checkAllowDownload(String resource)
{
public static boolean checkAllowDownload(String resource) {
// 禁止目录上跳级别
if (StringUtils.contains(resource, ".."))
{
if (StringUtils.contains(resource, "..")) {
return false;
}
// 判断是否在允许下载的文件规则内
@ -142,28 +126,20 @@ public class FileUtils
* @param fileName 文件名
* @return 编码后的文件名
*/
public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
{
public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
final String agent = request.getHeader("USER-AGENT");
String filename = fileName;
if (agent.contains("MSIE"))
{
if (agent.contains("MSIE")) {
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
}
else if (agent.contains("Firefox"))
{
} else if (agent.contains("Firefox")) {
// 火狐浏览器
filename = new String(fileName.getBytes(), "ISO8859-1");
}
else if (agent.contains("Chrome"))
{
} else if (agent.contains("Chrome")) {
// google浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
else
{
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
@ -176,30 +152,24 @@ public class FileUtils
* @param filePath 文件
* @return 文件名
*/
public static String getName(String filePath)
{
if (null == filePath)
{
public static String getName(String filePath) {
if (null == filePath) {
return null;
}
int len = filePath.length();
if (0 == len)
{
if (0 == len) {
return filePath;
}
if (isFileSeparator(filePath.charAt(len - 1)))
{
if (isFileSeparator(filePath.charAt(len - 1))) {
// 以分隔符结尾的去掉结尾分隔符
len--;
}
int begin = 0;
char c;
for (int i = len - 1; i > -1; i--)
{
for (int i = len - 1; i > -1; i--) {
c = filePath.charAt(i);
if (isFileSeparator(c))
{
if (isFileSeparator(c)) {
// 查找最后一个路径分隔符/或者\
begin = i + 1;
break;
@ -216,8 +186,7 @@ public class FileUtils
* @param c 字符
* @return 是否为Windows或者LinuxUnix文件分隔符
*/
public static boolean isFileSeparator(char c)
{
public static boolean isFileSeparator(char c) {
return SLASH == c || BACKSLASH == c;
}
@ -227,8 +196,7 @@ public class FileUtils
* @param response 响应对象
* @param realFileName 真实文件名
*/
public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
{
public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder();
@ -249,8 +217,7 @@ public class FileUtils
* @param s 需要百分号编码的字符串
* @return 百分号编码后的字符串
*/
public static String percentEncode(String s) throws UnsupportedEncodingException
{
public static String percentEncode(String s) throws UnsupportedEncodingException {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
return encode.replaceAll("\\+", "%20");
}
@ -294,7 +261,7 @@ public class FileUtils
if (fileName == null) {
return null;
}
String prefix = fileName.substring(0, fileName.lastIndexOf("."));
String prefix = UUID.randomUUID().toString().replace("-", "");
String suffix = fileName.substring(fileName.lastIndexOf("."));
File file = File.createTempFile(prefix, suffix);
multiFile.transferTo(file);

View File

@ -1,7 +1,6 @@
package com.bonus.file.controller;
import com.bonus.common.core.utils.Base64Utils;
import com.bonus.file.utils.FileDownloadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;