5 changed files with 164 additions and 0 deletions
-
1cclqms-java/src/main/java/com/gaotao/config/ShiroConfig.java
-
38cclqms-java/src/main/java/com/gaotao/modules/reader/controller/ClientFileCollectorController.java
-
36cclqms-java/src/main/java/com/gaotao/modules/reader/data/CollectorUploadResultData.java
-
12cclqms-java/src/main/java/com/gaotao/modules/reader/service/ClientFileCollectorService.java
-
77cclqms-java/src/main/java/com/gaotao/modules/reader/service/impl/ClientFileCollectorServiceImpl.java
@ -0,0 +1,38 @@ |
|||||
|
package com.gaotao.modules.reader.controller; |
||||
|
|
||||
|
import com.gaotao.common.utils.R; |
||||
|
import com.gaotao.modules.reader.data.CollectorUploadResultData; |
||||
|
import com.gaotao.modules.reader.service.ClientFileCollectorService; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
@Slf4j |
||||
|
@RestController |
||||
|
@RequestMapping("/collector/client") |
||||
|
public class ClientFileCollectorController { |
||||
|
|
||||
|
@Autowired |
||||
|
private ClientFileCollectorService clientFileCollectorService; |
||||
|
|
||||
|
/** |
||||
|
* 客户端按设备目录上传文件 |
||||
|
*/ |
||||
|
@PostMapping("/upload") |
||||
|
public R upload(@RequestParam("file") MultipartFile file, |
||||
|
@RequestParam("site") String site, |
||||
|
@RequestParam("buNo") String buNo, |
||||
|
@RequestParam("equipmentNo") String equipmentNo) { |
||||
|
try { |
||||
|
CollectorUploadResultData result = clientFileCollectorService.uploadFile(file, site, buNo, equipmentNo); |
||||
|
return R.ok("上传成功").put("data", result); |
||||
|
} catch (Exception e) { |
||||
|
log.error("客户端文件上传失败: {}", e.getMessage(), e); |
||||
|
return R.error("客户端文件上传失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
package com.gaotao.modules.reader.data; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class CollectorUploadResultData { |
||||
|
/** |
||||
|
* 站点 |
||||
|
*/ |
||||
|
private String site; |
||||
|
|
||||
|
/** |
||||
|
* 业务单元 |
||||
|
*/ |
||||
|
private String buNo; |
||||
|
|
||||
|
/** |
||||
|
* 设备编码 |
||||
|
*/ |
||||
|
private String equipmentNo; |
||||
|
|
||||
|
/** |
||||
|
* 服务器保存后的文件名 |
||||
|
*/ |
||||
|
private String savedFileName; |
||||
|
|
||||
|
/** |
||||
|
* 服务器完整保存路径 |
||||
|
*/ |
||||
|
private String savedFullPath; |
||||
|
|
||||
|
/** |
||||
|
* 上传文件大小 |
||||
|
*/ |
||||
|
private long fileSize; |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
package com.gaotao.modules.reader.service; |
||||
|
|
||||
|
import com.gaotao.modules.reader.data.CollectorUploadResultData; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
public interface ClientFileCollectorService { |
||||
|
|
||||
|
/** |
||||
|
* 客户端上传文件 |
||||
|
*/ |
||||
|
CollectorUploadResultData uploadFile(MultipartFile file, String site, String buNo, String equipmentNo); |
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
package com.gaotao.modules.reader.service.impl; |
||||
|
|
||||
|
import com.gaotao.modules.reader.data.CollectorUploadResultData; |
||||
|
import com.gaotao.modules.reader.service.ClientFileCollectorService; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Path; |
||||
|
import java.nio.file.Paths; |
||||
|
import java.nio.file.StandardCopyOption; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
public class ClientFileCollectorServiceImpl implements ClientFileCollectorService { |
||||
|
|
||||
|
/** |
||||
|
* 服务端统一落盘目录:D:\qms-datcol |
||||
|
*/ |
||||
|
private static final Path SERVER_ROOT_PATH = Paths.get("D:\\qms-datcol"); |
||||
|
|
||||
|
@Override |
||||
|
public CollectorUploadResultData uploadFile(MultipartFile file, String site, String buNo, String equipmentNo) { |
||||
|
if (file == null || file.isEmpty()) { |
||||
|
throw new IllegalArgumentException("上传文件不能为空"); |
||||
|
} |
||||
|
if (StringUtils.isBlank(site) || StringUtils.isBlank(buNo) || StringUtils.isBlank(equipmentNo)) { |
||||
|
throw new IllegalArgumentException("site、buNo、equipmentNo不能为空"); |
||||
|
} |
||||
|
|
||||
|
String safeEquipmentNo = this.toSafeFolderName(equipmentNo); |
||||
|
if (StringUtils.isBlank(safeEquipmentNo)) { |
||||
|
throw new IllegalArgumentException("equipmentNo包含非法字符,无法创建目录"); |
||||
|
} |
||||
|
|
||||
|
String originalFileName = StringUtils.defaultIfBlank(file.getOriginalFilename(), UUID.randomUUID() + ".dat"); |
||||
|
String safeFileName = this.toSafeFileName(originalFileName); |
||||
|
Path equipmentFolderPath = SERVER_ROOT_PATH.resolve(safeEquipmentNo); |
||||
|
Path targetPath = equipmentFolderPath.resolve(safeFileName); |
||||
|
try { |
||||
|
Files.createDirectories(equipmentFolderPath); |
||||
|
try (InputStream inputStream = file.getInputStream()) { |
||||
|
Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING); |
||||
|
} |
||||
|
log.info("客户端文件上传成功: site={}, buNo={}, equipmentNo={}, path={}", |
||||
|
site, buNo, equipmentNo, targetPath); |
||||
|
} catch (IOException e) { |
||||
|
log.error("保存客户端上传文件失败: {}", e.getMessage(), e); |
||||
|
throw new RuntimeException("保存上传文件失败: " + e.getMessage()); |
||||
|
} |
||||
|
CollectorUploadResultData result = new CollectorUploadResultData(); |
||||
|
result.setSite(site); |
||||
|
result.setBuNo(buNo); |
||||
|
result.setEquipmentNo(equipmentNo); |
||||
|
result.setSavedFileName(safeFileName); |
||||
|
result.setSavedFullPath(targetPath.toString()); |
||||
|
result.setFileSize(file.getSize()); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
private String toSafeFileName(String fileName) { |
||||
|
String safeFileName = fileName.replaceAll("[\\\\/:*?\"<>|]", "_").trim(); |
||||
|
if (StringUtils.isBlank(safeFileName)) { |
||||
|
return UUID.randomUUID() + ".dat"; |
||||
|
} |
||||
|
return safeFileName; |
||||
|
} |
||||
|
|
||||
|
private String toSafeFolderName(String folderName) { |
||||
|
return folderName.replaceAll("[\\\\/:*?\"<>|]", "_").trim(); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue