|
@@ -0,0 +1,98 @@
|
|
|
+package com.judong.chuanyiserver.util;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileTypeUtil;
|
|
|
+import com.judong.chuanyiserver.dao.FileDao;
|
|
|
+import com.judong.chuanyiserver.entity.FileEntity;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@Transactional
|
|
|
+public class FileUtil {
|
|
|
+
|
|
|
+ @Value("${file.path}")
|
|
|
+ private String filePath;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FileDao fileDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传单个上传文件
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public void fileOneUpload(MultipartFile file) {
|
|
|
+ if (Blank.isEmpty(file)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ Date date = new Date();//现在的日期
|
|
|
+ cal.setTime(date);
|
|
|
+ Integer year = cal.get(Calendar.YEAR);//获取年
|
|
|
+ Integer month = cal.get(Calendar.MONTH) + 1;//获取月(月份从0开始,如果按照中国的习惯,需要加一)
|
|
|
+ Integer day_moneth = cal.get(Calendar.DAY_OF_MONTH);//获取日(月中的某一天)
|
|
|
+ String fileSavePath = filePath + "\\" + year + "\\" + month + "\\" + day_moneth;
|
|
|
+ File file1 = new File(fileSavePath);
|
|
|
+ if (!file1.exists() && !file1.isDirectory()) {
|
|
|
+ file1.mkdirs();
|
|
|
+ }
|
|
|
+ String originalFileName = file.getOriginalFilename();
|
|
|
+ String suffixName = FileTypeUtil.getType((File) file);
|
|
|
+ String saveFileName = (new Date()).getTime() + ((int) (Math.random() * 90000) + 10000) + "." + suffixName;
|
|
|
+ FileEntity fileEntity = new FileEntity(fileSavePath, originalFileName, saveFileName, suffixName);
|
|
|
+ if (fileDao.saveFile(fileEntity) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File newFile = new File(fileSavePath + "\\" + saveFileName);
|
|
|
+ try {
|
|
|
+ file.transferTo(newFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传多个文件
|
|
|
+ *
|
|
|
+ * @param files
|
|
|
+ */
|
|
|
+ public void fileManyUpload(MultipartFile[] files) {
|
|
|
+ if (Blank.isEmpty(files)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ Date date = new Date();//现在的日期
|
|
|
+ cal.setTime(date);
|
|
|
+ Integer year = cal.get(Calendar.YEAR);//获取年
|
|
|
+ Integer month = cal.get(Calendar.MONTH) + 1;//获取月(月份从0开始,如果按照中国的习惯,需要加一)
|
|
|
+ Integer day_moneth = cal.get(Calendar.DAY_OF_MONTH);//获取日(月中的某一天)
|
|
|
+// Integer day_week=cal.get(Calendar.DAY_OF_WEEK);//获取一周内的某一天
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ String fileSavePath = filePath + "\\" + year + "\\" + month + "\\" + day_moneth;
|
|
|
+ String originalFileName = file.getOriginalFilename();
|
|
|
+ String suffixName = FileTypeUtil.getType((File) file);
|
|
|
+ String saveFileName = (new Date()).getTime() + ((int) (Math.random() * 90000) + 10000) + "." + suffixName;
|
|
|
+ FileEntity fileEntity = new FileEntity(fileSavePath, originalFileName, saveFileName, suffixName);
|
|
|
+ Integer integer = fileDao.saveFile(fileEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void fileUpload(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void fileDownload(MultipartFile[] files) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|