Procházet zdrojové kódy

图片文件移动处理

anderx před 3 roky
rodič
revize
e519f6b359

+ 121 - 0
src/main/java/com/goafanti/common/controller/TestPublicController.java

@@ -0,0 +1,121 @@
+package com.goafanti.common.controller;
+
+import com.goafanti.admin.service.AdminService;
+import com.goafanti.admin.service.AttachmentService;
+import com.goafanti.common.bo.Error;
+import com.goafanti.common.bo.OutUser;
+import com.goafanti.common.bo.Result;
+import com.goafanti.common.constant.AFTConstants;
+import com.goafanti.common.constant.ErrorConstants;
+import com.goafanti.common.dao.TOrderMidMapper;
+import com.goafanti.common.enums.UserType;
+import com.goafanti.common.model.User;
+import com.goafanti.common.service.DistrictGlossoryService;
+import com.goafanti.common.service.IndustryCategoryService;
+import com.goafanti.common.utils.*;
+import com.goafanti.common.utils.excel.FileUtils;
+import com.goafanti.common.utils.excel.NewExcelUtil;
+import com.goafanti.core.shiro.token.TokenManager;
+import com.goafanti.core.websocket.SystemWebSocketHandler;
+import com.goafanti.order.service.OrderProjectService;
+import com.goafanti.user.service.UserService;
+import org.apache.commons.collections4.map.HashedMap;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Scope;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.socket.TextMessage;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.util.*;
+
+@Controller
+@RequestMapping("/open")
+public class TestPublicController extends CertifyApiController {
+
+    @Value(value = "${upload.path}")
+    private String		uploadPath			= null;
+
+    private String		targePath			="F:/data/public/upload/new_order_file";
+
+
+    @RequestMapping("/pushOrder")
+    public Result pushOrder() {
+      Result res= new Result();
+        // 源文件夹路径(存放图片路径)
+        String url=uploadPath+"/order_file/19e76b19-7db5-41e6-b75d-80c0fe20a35c/20719309158800.jpg";
+        String name=url.substring(url.lastIndexOf("/"),url.length());
+        String targeUrl=targePath+name;
+        //目标文件夹路径(没有会自动创建)
+        File targetFile = new File(targePath);
+        File srcFile = new File(url);
+        File destFile = new File(targeUrl);
+        if (!targetFile.exists()){
+            targetFile.mkdir();
+        }
+        try {
+            copyFile(srcFile,destFile);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return res;
+    }
+
+
+
+    public static String SUFFIX = null;
+
+    private static  void moveFile2(File sourceFile,File targetFile) {
+        //该方法为复制文件,sourceFile源文件夹路径(存放图片路径), targetFile是目标文件夹路径(没有会自动创建)
+        File[] fileArray1 = sourceFile.listFiles();
+        File[] fileArray2 = targetFile.listFiles();
+        for (File file : fileArray1) {
+//	        	SUFFIX用于判断指定图片是否存在,如果存在就不移动并且输出“文件已存在”
+            if (file.getName().contentEquals(SUFFIX)) {
+                targetFile = new File( targetFile + "\\"+file.getName());
+                if (targetFile.exists()) {
+                    System.out.println("文件已存在");
+                } else {
+                    file.renameTo(targetFile);
+                    System.out.println("移动文件成功");
+                }
+            }
+        }
+    }
+
+
+    private static void copyFile(File srcFile,File destFile) throws IOException {
+        //该方法为复制文件,srcFile是原文件所在地址,destFile是目标文件所在地址
+
+        File[] fileArray1 = srcFile.listFiles();
+        File[] fileArray2 = destFile.listFiles();
+
+        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
+        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
+        byte[] bys = new byte[1024];
+        int len = 0;
+        while ((len = bis.read(bys)) != -1)
+        {
+            bos.write(bys, 0, len);
+        }
+        bos.close();
+        bis.close();
+    }
+
+
+}

+ 34 - 14
src/main/java/com/goafanti/common/utils/excel/FileUtils.java

@@ -1,11 +1,6 @@
 package com.goafanti.common.utils.excel;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import javax.servlet.http.HttpServletRequest;
@@ -16,13 +11,13 @@ import com.goafanti.common.utils.StringUtils;
 
 /**
  * 文件处理工具类
- * 
+ *
  * @author ruoyi
  */
 public class FileUtils extends org.apache.commons.io.FileUtils
 {
     public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
-    
+
     public static final String[] DEFAULT_ALLOWED_EXTENSION = {
             // 图片
             "bmp", "gif", "jpg", "jpeg", "png",
@@ -35,7 +30,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
 
     /**
      * 输出指定文件的byte数组
-     * 
+     *
      * @param filePath 文件路径
      * @param os 输出流
      * @return
@@ -91,7 +86,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
 
     /**
      * 删除文件
-     * 
+     *
      * @param filePath 文件
      * @return
      */
@@ -108,9 +103,34 @@ public class FileUtils extends org.apache.commons.io.FileUtils
         return flag;
     }
 
+
+    /**
+     * 复制文件
+     * @param srcFile 源文件
+     * @param destFile 目标文件
+     * @throws IOException
+     */
+    public static void copyFile(File srcFile,File destFile) throws IOException {
+        //该方法为复制文件,srcFile是原文件所在地址,destFile是目标文件所在地址
+
+        File[] fileArray1 = srcFile.listFiles();
+        File[] fileArray2 = destFile.listFiles();
+
+        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
+        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
+        byte[] bys = new byte[1024];
+        int len = 0;
+        while ((len = bis.read(bys)) != -1)
+        {
+            bos.write(bys, 0, len);
+        }
+        bos.close();
+        bis.close();
+    }
+
     /**
      * 文件名称验证
-     * 
+     *
      * @param filename 文件名称
      * @return true 正常 false 非法
      */
@@ -121,7 +141,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
 
     /**
      * 检查文件是否可下载
-     * 
+     *
      * @param resource 需要下载的文件
      * @return true 正常 false 非法
      */
@@ -145,7 +165,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
 
     /**
      * 下载文件名重新编码
-     * 
+     *
      * @param request 请求对象
      * @param fileName 文件名
      * @return 编码后的文件名
@@ -215,7 +235,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
         String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
         return encode.replaceAll("\\+", "%20");
     }
-    
+
     /**
      * 获取文件类型
      * <p>