|
@@ -1,252 +1,252 @@
|
|
|
-package com.fd.server.impl;
|
|
|
-
|
|
|
-import com.fd.constant.MsgCode;
|
|
|
-import com.fd.dto.PageDto;
|
|
|
-import com.fd.entity.FileEntity;
|
|
|
-import com.fd.repository.FileRepository;
|
|
|
-import com.fd.server.FileServer;
|
|
|
-import com.fd.util.FileUtils;
|
|
|
-import com.fd.util.R;
|
|
|
-import lombok.extern.log4j.Log4j2;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
-import org.springframework.data.domain.PageRequest;
|
|
|
-import org.springframework.data.domain.Sort;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import javax.transaction.Transactional;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.Base64;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.Optional;
|
|
|
-
|
|
|
-/**
|
|
|
- * Created by Owen on 2019/11/12 0012 10:05
|
|
|
- */
|
|
|
-@Log4j2
|
|
|
-@Service
|
|
|
-@Transactional
|
|
|
-public class FileServerImpl implements FileServer {
|
|
|
-
|
|
|
- @Value("${input.file.path}")
|
|
|
- private String INPUT_FILE_PATH;
|
|
|
-
|
|
|
- @Value("${output.file.path}")
|
|
|
- private String OUTPUT_FILE_PATH;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private FileRepository fileRepository;
|
|
|
-
|
|
|
- @Override
|
|
|
- public R uploadBigFile(MultipartFile file, String type) {
|
|
|
- log.warn("run uploadBigFile");
|
|
|
- long start = System.currentTimeMillis();
|
|
|
- if (file.isEmpty() || file.getSize() <= 0) {
|
|
|
- log.info("文件为空");
|
|
|
- return new R(50001, MsgCode.E50001);
|
|
|
- }
|
|
|
-
|
|
|
- // 文件名全名
|
|
|
- String fullFileName = file.getOriginalFilename();
|
|
|
-
|
|
|
- // 创建目录路径
|
|
|
- FileUtils.createDir(INPUT_FILE_PATH);
|
|
|
-
|
|
|
- // 拼接唯一文件名
|
|
|
- long timeMillis = System.currentTimeMillis();
|
|
|
-// String fileName = timeMillis + "_" + fullFileName;
|
|
|
- String fileName = fullFileName;
|
|
|
-
|
|
|
- // 文件保存路径
|
|
|
- String filePath = INPUT_FILE_PATH + fileName;
|
|
|
-
|
|
|
- // 写文件到本地
|
|
|
- try {
|
|
|
- FileUtils.bigFileWrite(file.getInputStream(), filePath);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- log.info("filePath: {}", filePath);
|
|
|
-
|
|
|
- // 保存信息到db
|
|
|
- FileEntity entity = new FileEntity();
|
|
|
- entity.setFileName(fileName);
|
|
|
- entity.setFileUrl(filePath);
|
|
|
- entity.setCreateTime(new Date());
|
|
|
- entity.setUpdateTime(new Date());
|
|
|
- entity.setType(type);
|
|
|
- entity.setStatus(1);
|
|
|
- fileRepository.save(entity);
|
|
|
- long end = System.currentTimeMillis();
|
|
|
- log.info("end uploadBigFile, total time: {} s", (end - start)/1000);
|
|
|
- return new R(200, entity);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public R uploadRasterBigFile(MultipartFile file, String type) {
|
|
|
- log.warn("run uploadBigFile");
|
|
|
- long start = System.currentTimeMillis();
|
|
|
- if (file.isEmpty() || file.getSize() <= 0) {
|
|
|
- log.info("文件为空");
|
|
|
- return new R(50001, MsgCode.E50001);
|
|
|
- }
|
|
|
-
|
|
|
- // 文件名全名
|
|
|
- String fullFileName = file.getOriginalFilename();
|
|
|
-
|
|
|
- // 创建目录路径
|
|
|
- FileUtils.createDir(INPUT_FILE_PATH);
|
|
|
-
|
|
|
- // 拼接唯一文件名
|
|
|
- long timeMillis = System.currentTimeMillis();
|
|
|
-// String fileName = timeMillis + "_" + fullFileName;
|
|
|
- String fileName = fullFileName;
|
|
|
-
|
|
|
- // 文件保存路径
|
|
|
- String filePath = INPUT_FILE_PATH + fileName;
|
|
|
-
|
|
|
- // 写文件到本地
|
|
|
-
|
|
|
- try {
|
|
|
- FileUtils.bigFileWrite(file.getInputStream(), filePath);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- log.info("filePath: {}", filePath);
|
|
|
-
|
|
|
- // 保存信息到db
|
|
|
- FileEntity entity = new FileEntity();
|
|
|
- entity.setFileName(fileName);
|
|
|
- entity.setFileUrl(filePath);
|
|
|
- entity.setCreateTime(new Date());
|
|
|
- entity.setUpdateTime(new Date());
|
|
|
- entity.setType(type);
|
|
|
- entity.setStatus(2);
|
|
|
- fileRepository.save(entity);
|
|
|
- long end = System.currentTimeMillis();
|
|
|
- log.info("end uploadBigFile, total time: {} s", (end - start)/1000);
|
|
|
- return new R(200, entity);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public R uploadFile(MultipartFile file, String directoryName, String type) {
|
|
|
- if (file.isEmpty() || file.getSize() <= 0) {
|
|
|
- log.info("文件为空");
|
|
|
- return new R(50001, MsgCode.E50001);
|
|
|
- }
|
|
|
-
|
|
|
- // 文件名全名
|
|
|
- String fullFileName = file.getOriginalFilename();
|
|
|
-
|
|
|
- // 创建目录路径
|
|
|
- FileUtils.createDir(INPUT_FILE_PATH + directoryName);
|
|
|
-
|
|
|
- // 拼接唯一文件名
|
|
|
-// String fileName = FileUtils.dateStr() + fullFileName;
|
|
|
-
|
|
|
- // 文件保存路径
|
|
|
- String filePath = INPUT_FILE_PATH + directoryName + File.separator + fullFileName;
|
|
|
- log.info("filePath: {}", filePath);
|
|
|
-
|
|
|
- // 写文件到本地
|
|
|
- try {
|
|
|
- byte[] bytes = file.getBytes();
|
|
|
- String content = Base64.getEncoder().encodeToString(bytes);
|
|
|
- FileUtils.base64ToFileWriter(content, filePath);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- // 保存信息到db
|
|
|
- FileEntity entity = new FileEntity();
|
|
|
- entity.setFileName(fullFileName);
|
|
|
- entity.setFileUrl(filePath);
|
|
|
- entity.setCreateTime(new Date());
|
|
|
- entity.setUpdateTime(new Date());
|
|
|
- entity.setType(type);
|
|
|
- fileRepository.save(entity);
|
|
|
-
|
|
|
- log.info("end uploadBigFile");
|
|
|
- return new R(200, entity);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public R findByType(String type, PageDto pageDto) {
|
|
|
- Page<FileEntity> page = fileRepository.findByType(type, PageRequest.of(pageDto.getPageNum(), pageDto.getPageSize(), Sort.by("createTime").descending()));
|
|
|
- return new R(200, page);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public R findByType(String type1, String type2, PageDto pageDto) {
|
|
|
- Page<FileEntity> page = fileRepository.findByType(type1, type2, PageRequest.of(pageDto.getPageNum(), pageDto.getPageSize(), Sort.by("createTime").descending()));
|
|
|
- return new R(200, page);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public R deleteById(Long fileId) {
|
|
|
- // 删除服务器文件
|
|
|
- Optional<FileEntity> e = fileRepository.findById(fileId);
|
|
|
- if (!e.isPresent()) {
|
|
|
- return new R(50002, MsgCode.E50002);
|
|
|
- }
|
|
|
- FileEntity fileEntity = e.get();
|
|
|
- String fileName = fileEntity.getFileName();
|
|
|
- // 文件
|
|
|
- if (fileName.contains(".")) {
|
|
|
- File file = new File(fileEntity.getFileUrl());
|
|
|
- file.delete();
|
|
|
- } else { // 目录
|
|
|
- FileUtils.delAllFile(fileEntity.getFileUrl());
|
|
|
- }
|
|
|
-
|
|
|
- // 删除数据库记录
|
|
|
- fileRepository.deleteById(fileId);
|
|
|
-
|
|
|
- return new R(200, MsgCode.SUCCESS);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Integer getGeoData(HttpServletResponse response, String filePath) {
|
|
|
- // 判断文件是否存在
|
|
|
- filePath = OUTPUT_FILE_PATH + filePath;
|
|
|
+//package com.fd.server.impl;
|
|
|
+//
|
|
|
+//import com.fd.constant.MsgCode;
|
|
|
+//import com.fd.dto.PageDto;
|
|
|
+//import com.fd.entity.FileEntity;
|
|
|
+//import com.fd.repository.FileRepository;
|
|
|
+//import com.fd.server.FileServer;
|
|
|
+//import com.fd.util.FileUtils;
|
|
|
+//import com.fd.util.R;
|
|
|
+//import lombok.extern.log4j.Log4j2;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.beans.factory.annotation.Value;
|
|
|
+//import org.springframework.data.domain.Page;
|
|
|
+//import org.springframework.data.domain.PageRequest;
|
|
|
+//import org.springframework.data.domain.Sort;
|
|
|
+//import org.springframework.stereotype.Service;
|
|
|
+//import org.springframework.web.multipart.MultipartFile;
|
|
|
+//
|
|
|
+//import javax.servlet.http.HttpServletResponse;
|
|
|
+//import javax.transaction.Transactional;
|
|
|
+//import java.io.File;
|
|
|
+//import java.io.IOException;
|
|
|
+//import java.io.InputStream;
|
|
|
+//import java.util.Base64;
|
|
|
+//import java.util.Date;
|
|
|
+//import java.util.Optional;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * Created by Owen on 2019/11/12 0012 10:05
|
|
|
+// */
|
|
|
+//@Log4j2
|
|
|
+//@Service
|
|
|
+//@Transactional
|
|
|
+//public class FileServerImpl implements FileServer {
|
|
|
+//
|
|
|
+// @Value("${input.file.path}")
|
|
|
+// private String INPUT_FILE_PATH;
|
|
|
+//
|
|
|
+// @Value("${output.file.path}")
|
|
|
+// private String OUTPUT_FILE_PATH;
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private FileRepository fileRepository;
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public R uploadBigFile(MultipartFile file, String type) {
|
|
|
+// log.warn("run uploadBigFile");
|
|
|
+// long start = System.currentTimeMillis();
|
|
|
+// if (file.isEmpty() || file.getSize() <= 0) {
|
|
|
+// log.info("文件为空");
|
|
|
+// return new R(50001, MsgCode.E50001);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 文件名全名
|
|
|
+// String fullFileName = file.getOriginalFilename();
|
|
|
+//
|
|
|
+// // 创建目录路径
|
|
|
+// FileUtils.createDir(INPUT_FILE_PATH);
|
|
|
+//
|
|
|
+// // 拼接唯一文件名
|
|
|
+// long timeMillis = System.currentTimeMillis();
|
|
|
+//// String fileName = timeMillis + "_" + fullFileName;
|
|
|
+// String fileName = fullFileName;
|
|
|
+//
|
|
|
+// // 文件保存路径
|
|
|
+// String filePath = INPUT_FILE_PATH + fileName;
|
|
|
+//
|
|
|
+// // 写文件到本地
|
|
|
+// try {
|
|
|
+// FileUtils.bigFileWrite(file.getInputStream(), filePath);
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
// log.info("filePath: {}", filePath);
|
|
|
- File file = new File(filePath);
|
|
|
- if (!file.exists()) {
|
|
|
-// log.info("文件不存在: {}", filePath);
|
|
|
- return null;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- FileUtils.fileDownload(response, filePath);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- // 文件下载,不能有返回值
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FileEntity findById(Long fileId) {
|
|
|
- Optional<FileEntity> o = fileRepository.findById(fileId);
|
|
|
- if (!o.isPresent()) {
|
|
|
- log.info("id:{} 不存在");
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- FileEntity entity = o.get();
|
|
|
- return entity;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public FileEntity save(FileEntity entity) {
|
|
|
- return fileRepository.save(entity);
|
|
|
- }
|
|
|
-}
|
|
|
+//
|
|
|
+// // 保存信息到db
|
|
|
+// FileEntity entity = new FileEntity();
|
|
|
+// entity.setFileName(fileName);
|
|
|
+// entity.setFileUrl(filePath);
|
|
|
+// entity.setCreateTime(new Date());
|
|
|
+// entity.setUpdateTime(new Date());
|
|
|
+// entity.setType(type);
|
|
|
+// entity.setStatus(1);
|
|
|
+// fileRepository.save(entity);
|
|
|
+// long end = System.currentTimeMillis();
|
|
|
+// log.info("end uploadBigFile, total time: {} s", (end - start)/1000);
|
|
|
+// return new R(200, entity);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public R uploadRasterBigFile(MultipartFile file, String type) {
|
|
|
+// log.warn("run uploadBigFile");
|
|
|
+// long start = System.currentTimeMillis();
|
|
|
+// if (file.isEmpty() || file.getSize() <= 0) {
|
|
|
+// log.info("文件为空");
|
|
|
+// return new R(50001, MsgCode.E50001);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 文件名全名
|
|
|
+// String fullFileName = file.getOriginalFilename();
|
|
|
+//
|
|
|
+// // 创建目录路径
|
|
|
+// FileUtils.createDir(INPUT_FILE_PATH);
|
|
|
+//
|
|
|
+// // 拼接唯一文件名
|
|
|
+// long timeMillis = System.currentTimeMillis();
|
|
|
+//// String fileName = timeMillis + "_" + fullFileName;
|
|
|
+// String fileName = fullFileName;
|
|
|
+//
|
|
|
+// // 文件保存路径
|
|
|
+// String filePath = INPUT_FILE_PATH + fileName;
|
|
|
+//
|
|
|
+// // 写文件到本地
|
|
|
+//
|
|
|
+// try {
|
|
|
+// FileUtils.bigFileWrite(file.getInputStream(), filePath);
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// log.info("filePath: {}", filePath);
|
|
|
+//
|
|
|
+// // 保存信息到db
|
|
|
+// FileEntity entity = new FileEntity();
|
|
|
+// entity.setFileName(fileName);
|
|
|
+// entity.setFileUrl(filePath);
|
|
|
+// entity.setCreateTime(new Date());
|
|
|
+// entity.setUpdateTime(new Date());
|
|
|
+// entity.setType(type);
|
|
|
+// entity.setStatus(2);
|
|
|
+// fileRepository.save(entity);
|
|
|
+// long end = System.currentTimeMillis();
|
|
|
+// log.info("end uploadBigFile, total time: {} s", (end - start)/1000);
|
|
|
+// return new R(200, entity);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public R uploadFile(MultipartFile file, String directoryName, String type) {
|
|
|
+// if (file.isEmpty() || file.getSize() <= 0) {
|
|
|
+// log.info("文件为空");
|
|
|
+// return new R(50001, MsgCode.E50001);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 文件名全名
|
|
|
+// String fullFileName = file.getOriginalFilename();
|
|
|
+//
|
|
|
+// // 创建目录路径
|
|
|
+// FileUtils.createDir(INPUT_FILE_PATH + directoryName);
|
|
|
+//
|
|
|
+// // 拼接唯一文件名
|
|
|
+//// String fileName = FileUtils.dateStr() + fullFileName;
|
|
|
+//
|
|
|
+// // 文件保存路径
|
|
|
+// String filePath = INPUT_FILE_PATH + directoryName + File.separator + fullFileName;
|
|
|
+// log.info("filePath: {}", filePath);
|
|
|
+//
|
|
|
+// // 写文件到本地
|
|
|
+// try {
|
|
|
+// byte[] bytes = file.getBytes();
|
|
|
+// String content = Base64.getEncoder().encodeToString(bytes);
|
|
|
+// FileUtils.base64ToFileWriter(content, filePath);
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 保存信息到db
|
|
|
+// FileEntity entity = new FileEntity();
|
|
|
+// entity.setFileName(fullFileName);
|
|
|
+// entity.setFileUrl(filePath);
|
|
|
+// entity.setCreateTime(new Date());
|
|
|
+// entity.setUpdateTime(new Date());
|
|
|
+// entity.setType(type);
|
|
|
+// fileRepository.save(entity);
|
|
|
+//
|
|
|
+// log.info("end uploadBigFile");
|
|
|
+// return new R(200, entity);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public R findByType(String type, PageDto pageDto) {
|
|
|
+// Page<FileEntity> page = fileRepository.findByType(type, PageRequest.of(pageDto.getPageNum(), pageDto.getPageSize(), Sort.by("createTime").descending()));
|
|
|
+// return new R(200, page);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public R findByType(String type1, String type2, PageDto pageDto) {
|
|
|
+// Page<FileEntity> page = fileRepository.findByType(type1, type2, PageRequest.of(pageDto.getPageNum(), pageDto.getPageSize(), Sort.by("createTime").descending()));
|
|
|
+// return new R(200, page);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public R deleteById(Long fileId) {
|
|
|
+// // 删除服务器文件
|
|
|
+// Optional<FileEntity> e = fileRepository.findById(fileId);
|
|
|
+// if (!e.isPresent()) {
|
|
|
+// return new R(50002, MsgCode.E50002);
|
|
|
+// }
|
|
|
+// FileEntity fileEntity = e.get();
|
|
|
+// String fileName = fileEntity.getFileName();
|
|
|
+// // 文件
|
|
|
+// if (fileName.contains(".")) {
|
|
|
+// File file = new File(fileEntity.getFileUrl());
|
|
|
+// file.delete();
|
|
|
+// } else { // 目录
|
|
|
+// FileUtils.delAllFile(fileEntity.getFileUrl());
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 删除数据库记录
|
|
|
+// fileRepository.deleteById(fileId);
|
|
|
+//
|
|
|
+// return new R(200, MsgCode.SUCCESS);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public Integer getGeoData(HttpServletResponse response, String filePath) {
|
|
|
+// // 判断文件是否存在
|
|
|
+// filePath = OUTPUT_FILE_PATH + filePath;
|
|
|
+//// log.info("filePath: {}", filePath);
|
|
|
+// File file = new File(filePath);
|
|
|
+// if (!file.exists()) {
|
|
|
+//// log.info("文件不存在: {}", filePath);
|
|
|
+// return null;
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// try {
|
|
|
+// FileUtils.fileDownload(response, filePath);
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// // 文件下载,不能有返回值
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public FileEntity findById(Long fileId) {
|
|
|
+// Optional<FileEntity> o = fileRepository.findById(fileId);
|
|
|
+// if (!o.isPresent()) {
|
|
|
+// log.info("id:{} 不存在");
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+//
|
|
|
+// FileEntity entity = o.get();
|
|
|
+// return entity;
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public FileEntity save(FileEntity entity) {
|
|
|
+// return fileRepository.save(entity);
|
|
|
+// }
|
|
|
+//}
|