From 59e91a4e9ddaf23cebb12993c774aa899ab22d16 Mon Sep 17 00:00:00 2001
From: 郑永安 <zyazyz250@sina.com>
Date: Mon, 19 Jun 2023 14:22:45 +0800
Subject: [PATCH] 描述
---
src/main/java/com/gk/firework/Domain/Utils/UploadUtil.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 49 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/gk/firework/Domain/Utils/UploadUtil.java b/src/main/java/com/gk/firework/Domain/Utils/UploadUtil.java
new file mode 100644
index 0000000..0481c24
--- /dev/null
+++ b/src/main/java/com/gk/firework/Domain/Utils/UploadUtil.java
@@ -0,0 +1,49 @@
+package com.gk.firework.Domain.Utils;
+
+import org.springframework.web.multipart.MultipartFile;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class UploadUtil {
+
+ public static String uploadFile(MultipartFile file,String filePath) throws Exception {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
+ String oldName = file.getOriginalFilename();
+ if (StringUtils.isBlank(oldName)) {
+ return "null";
+ }
+ String newName = sdf.format(new Date()) + oldName.substring(oldName.lastIndexOf("."));
+ File dest = new File(filePath, newName);
+ String url = newName;
+ if (!dest.getParentFile().exists()) {
+ boolean rel = dest.getParentFile().mkdirs();
+ if (!rel) {
+ throw new Exception("文件夹创建失败");
+ }
+ }
+ InputStream is = file.getInputStream();
+ OutputStream os = new FileOutputStream(dest);
+ try {
+ byte[] buffer = new byte[8 * 1024];
+ int bytesRead;
+ while ((bytesRead = is.read(buffer)) != -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+ } catch (Exception e) {
+ throw e;
+ } finally {
+ if (is != null) {
+ is.close();
+ }
+ if (os != null) {
+ os.close();
+ }
+ }
+
+ return url;
+ }
+}
--
Gitblit v1.9.2