From 602be3f64c3709f9773284c48ca8aead180d060f Mon Sep 17 00:00:00 2001
From: zmk <496160012@qq.com>
Date: 星期三, 29 五月 2024 12:21:06 +0800
Subject: [PATCH] 修改了上传文件的逻辑
---
javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/ProjectDataServiceImpl.java | 78 +++++++++++++++++++++++++++++---------
javaweb-plus/javaweb-cms/src/main/resources/templates/geo/projectData/upload.html | 3 -
2 files changed, 60 insertions(+), 21 deletions(-)
diff --git a/javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/ProjectDataServiceImpl.java b/javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/ProjectDataServiceImpl.java
index 030592f..12908f6 100644
--- a/javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/ProjectDataServiceImpl.java
+++ b/javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/ProjectDataServiceImpl.java
@@ -1,14 +1,17 @@
package com.javaweb.geo.service.impl;
import java.io.File;
+import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ZipUtil;
import com.javaweb.common.config.Global;
import com.javaweb.common.core.domain.AjaxResult;
+import com.javaweb.common.exception.BusinessException;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.IdGenerate;
import com.javaweb.common.utils.StringUtils;
@@ -46,6 +49,9 @@
//瑙e帇鏂囦欢
private static String ZIPUploadPath2 = Global.getProfile() + "\\" + zipfile2 + "\\";
+
+ //鍗曟枃浠跺瓨鍌ㄨ矾寰�
+ private static String ZIPUploadPath2SingleFile = Global.getProfile() + "\\" + zipfile2 + "\\img\\";
@Autowired
private ProjectDataMapper projectDataMapper;
@@ -130,12 +136,22 @@
return projectDataMapper.deleteProjectDataById(id);
}
+
+
@Override
- public AjaxResult uploadZIP(MultipartFile file, HttpServletRequest request) {
+ public AjaxResult uploadZIP(MultipartFile file, HttpServletRequest request) {
String originalFilename = file.getOriginalFilename();
+
//浼犲叆鐨勯捇瀛旂殑id
String projectDataId = request.getParameter("projectDataId");
+
+ //涓婁紶鍗曟枃浠�
+ if(ObjectUtil.isNotEmpty(originalFilename) && !originalFilename.endsWith(".zip")){
+ uploadSingleFile(file,projectDataId);
+ return AjaxResult.success("涓婁紶鎴愬姛");
+ }
+ //涓婁紶ZIP鏂囦欢
//鑾峰彇鍚庣紑.zip 淇濆瓨鐨勬枃浠跺悕
String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
@@ -161,19 +177,46 @@
moveFile(moveDir);
//鏇存柊鏁版嵁搴�
- updateFilePath(projectDataId, ZIPUploadPath2 + UID +"\\" + originalFilename.replace(substring, "") );
-
- ProjectData projectData = new ProjectData();
-
- //鏇存柊 projectData url
- String dataUrl = zipfile + "/" + UID +".zip";
- projectData.setDataUrl(dataUrl);
- projectData.setId(projectDataId);
- projectDataMapper.updateProjectData(projectData);
+ updateFilePath(projectDataId, ZIPUploadPath2 + UID );
FileUtil.del(new File(ZIPUploadPath + UID));
return AjaxResult.success("涓婁紶鎴愬姛");
+ }
+
+ //涓婁紶鍗曟枃浠�
+ private void uploadSingleFile(MultipartFile file,String projectDataId) {
+
+ ProjectData projectData = projectDataMapper.selectProjectDataById(projectDataId);
+ String projectId= projectData.getProjectId();
+
+ String originalFilename= file.getOriginalFilename();
+ //鑾峰彇鍚庣紑 淇濆瓨鐨勬枃浠跺悕
+ String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
+ String UID = IdGenerate.nextId();
+ String dFileName = UID + substring;
+
+ String filepath = ZIPUploadPath2SingleFile + dFileName;
+
+ File destFile = new File(filepath);
+
+ try{
+ file.transferTo(destFile);
+ }catch (Exception e){
+ throw new BusinessException("淇濆瓨澶辫触锛�",e);
+ }
+
+ String url = filepath.replace(Global.getProfile(),"").replace("\\","/");
+
+ HoleMedia media = new HoleMedia();
+ media.setIds(IdGenerate.nextId());
+ media.setRecordId(projectDataId);
+ media.setProjectId(projectId);
+ media.setName(originalFilename);
+ media.setInternetPath(url);
+ media.setCreateTime(DateUtils.getNowDate());
+ mediaMapper.insertHoleMedia(media);
+
}
//绉诲姩 鏂囦欢
@@ -189,17 +232,14 @@
ProjectData projectData = projectDataMapper.selectProjectDataById(projectDataId);
String projectId= projectData.getProjectId();
-
File fileDir = new File(dirpath);
+ List<File> files = FileUtil.loopFiles(fileDir);
- if (fileDir.isDirectory()) {
- File[] files = fileDir.listFiles();
- int len = files.length;
- for (int i = 0; i < len; i++) {
- String filename = files[i].getName();
- String filepath = files[i].getPath();
+ if(ObjectUtil.isNotEmpty(files)){
+ for (File file : files){
+ String filename = file.getName();
+ String filepath = file.getPath();
String url = filepath.replace(Global.getProfile(),"").replace("\\","/");
-
HoleMedia media = new HoleMedia();
media.setIds(IdGenerate.nextId());
media.setRecordId(projectDataId);
@@ -209,7 +249,7 @@
media.setCreateTime(DateUtils.getNowDate());
mediaMapper.insertHoleMedia(media);
}
- }
+ }
}
}
diff --git a/javaweb-plus/javaweb-cms/src/main/resources/templates/geo/projectData/upload.html b/javaweb-plus/javaweb-cms/src/main/resources/templates/geo/projectData/upload.html
index b02bb26..3f39655 100644
--- a/javaweb-plus/javaweb-cms/src/main/resources/templates/geo/projectData/upload.html
+++ b/javaweb-plus/javaweb-cms/src/main/resources/templates/geo/projectData/upload.html
@@ -13,7 +13,6 @@
<form id="formId" enctype="multipart/form-data">
<div class="select-list">
<ul>
-
<li>
<p>涓婁紶鏂囦欢锛�</p>
<input type="file" id="file" name="file"/>
@@ -43,7 +42,7 @@
function upload(){
var file = $('#file').val();
- if (file == '' || (!$.common.endWith(file, '.zip'))){
+ if (file == ''|| file ==null){
$.modal.msgWarning("璇烽�夋嫨寰呬笂浼犵殑鏂囦欢");
return false;
}
--
Gitblit v1.9.1