123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package com.miniframe.tools;
- import com.miniframe.constant.MFConstant;
- import com.miniframe.core.exception.BusinessException;
- import com.miniframe.core.ext.UtilTools;
- import com.miniframe.model.system.SysFile;
- import com.miniframe.model.system.dao.SysFileMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 大文件上传
- */
- public class CaeFileUtils {
- private static Logger logger = LoggerFactory.getLogger(CaeFileUtils.class);
- public static void main(String[] args) throws IOException {
- String t = XIDateTimeUtils.DATE_PATTERN_ZONE.format(XIDateTimeUtils.getNowZoned());
- logger.info(t);
- }
- public static void deleFile(String fileId){
- SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
- SysFile pFile = sysFileDAO.selectByPrimaryKey(fileId);
- if(pFile!=null){
- File file =new File(XIFileUtils.getRootPathStr()+ MFConstant.separator+pFile.getFilepath());
- file.delete();
- sysFileDAO.deleteByPrimaryKey(pFile.getId());
- }
- }
- /**
- * 创建 存储文件
- */
- public static String createBigFilePath(String fileName) throws IOException {
- String bigFileName =UtilTools.getUUid()+"-"+fileName;
- String newDir = XIDateTimeUtils.DATE_PATTERN_ZONE.format(XIDateTimeUtils.getNowZoned());
- String dirPath = XIFileUtils.getRootPathStr()+ MFConstant.separator+newDir;
- File file = new File(dirPath);
- if(!file.exists()){
- file.mkdirs();
- }
- String bigFilePath=dirPath+MFConstant.separator+bigFileName;
- File bigFile = new File(bigFilePath);
- if(!bigFile.exists()){
- bigFile.createNewFile();
- }
- return bigFilePath;
- }
- public static void saveBigFiles(String bigFileId,String pathStr,String userid,
- String fileMedia,String fileInfo,int chunks) throws IOException, BusinessException {
- SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
- SysFile sysFileyz = sysFileDAO.selectByPrimaryKey(bigFileId);
- if(sysFileyz!=null){
- throw new BusinessException("EB4000001");
- }
- Path path = Paths.get(pathStr);
- String fielRelativePath = XIFileUtils.getRelativizePathStr(path);
- SysFile sysFile = new SysFile();
- sysFile.setId(bigFileId);
- //去掉uid部分
- int pathIndex = path.getFileName().toString().indexOf("-");
- if (pathIndex >= 0 && pathIndex < path.getFileName().toString().length() - 1) {
- sysFile.setFilename(path.getFileName().toString().substring(pathIndex + 1));
- } else {
- sysFile.setFilename(path.getFileName().toString());
- }
- sysFile.setLasttime(XIDateTimeUtils.getNowDate());
- sysFile.setUid(userid);
- sysFile.setFilemedia(fileMedia);
- sysFile.setRemarks(fileInfo);
- sysFile.setFiletype(XIFileUtils.getFileType(path.getFileName().toString()));
- sysFile.setFilepath(fielRelativePath);
- sysFile.setChunk(-1);
- sysFile.setChunks(chunks);
- sysFile.setNeedop("-1");//未合并
- sysFileDAO.insertSelective(sysFile);
- }
- public static void saveFile(String bigFileId, String pathStr, String userid,
- String fileMedia, String fileInfo) throws IOException, BusinessException {
- SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
- SysFile sysFileyz = sysFileDAO.selectByPrimaryKey(bigFileId);
- if(sysFileyz!=null){
- throw new BusinessException("EB4000001");
- }
- Path path = Paths.get(pathStr);
- String fielRelativePath = XIFileUtils.getRelativizePathStr(path);
- SysFile sysFile = new SysFile();
- sysFile.setId(bigFileId);
- //去掉uid部分
- int pathIndex = path.getFileName().toString().indexOf("-");
- if (pathIndex >= 0 && pathIndex < path.getFileName().toString().length() - 1) {
- sysFile.setFilename(path.getFileName().toString().substring(pathIndex + 1));
- } else {
- sysFile.setFilename(path.getFileName().toString());
- }
- sysFile.setLasttime(XIDateTimeUtils.getNowDate());
- sysFile.setUid(userid);
- sysFile.setFilemedia(fileMedia);
- sysFile.setRemarks(fileInfo);
- sysFile.setFiletype(XIFileUtils.getFileType(path.getFileName().toString()));
- sysFile.setFilepath(fielRelativePath);
- sysFile.setChunk(1);
- sysFile.setChunks(1);
- sysFile.setNeedop("1");//合并
- sysFileDAO.insertSelective(sysFile);
- }
- /**
- * 以行为单位读取文件,常用于读面向行的格式化文件
- */
- public static ArrayList<String> readFileByLines(String fileName) {
- File file = new File(fileName);
- ArrayList<String> fileStr = new ArrayList<String>();
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- String tempString = null;
- while ((tempString = reader.readLine()) != null) {
- fileStr.add(tempString);
- }
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e1) {
- }
- }
- }
- return fileStr;
- }
- /**
- * 替换双空格
- * @param line
- * @return
- */
- public static String stringLessDouble(String line){
- line =line.trim();
- while (line.indexOf(" ")>0){
- line = line.replace(" "," ");
- }
- return line;
- }
- }
|