Explorar o código

批量下载,在文件名后+时间戳或存储名称,防止文件名相同只下载单个

user5 %!s(int64=4) %!d(string=hai) anos
pai
achega
36b518ae60
Modificáronse 1 ficheiros con 16 adicións e 4 borrados
  1. 16 4
      src/main/java/com/jeeplus/modules/utils/SftpClientUtil.java

+ 16 - 4
src/main/java/com/jeeplus/modules/utils/SftpClientUtil.java

@@ -308,13 +308,25 @@ public class SftpClientUtil {
      *
      * @param directory     SFTP服务器的文件路径
      * @param downloadFile  SFTP服务器上的文件名
-     * @return              字节数组
+     * @param downLoadFilePath   保存路径
+     * @param finalFileName   准备生成的文件名
+     * @return
+     * @throws IOException
      */
     public byte[] downloadRuralProject(String directory, String downloadFile,String downLoadFilePath,String finalFileName) throws IOException {
         File dirFile = new File(downLoadFilePath);
         if (!dirFile.exists()) {
             dirFile.mkdirs();
         }
+        //睡眠1毫秒 用来防止相同文件名被替换
+        try {
+            Thread.sleep(1);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        //文件名处理(将文件名拆分为名称和后缀名为之后添加时间戳做准备)
+        String headName = finalFileName.substring(0,finalFileName.lastIndexOf("."));
+        String endName = finalFileName.substring(finalFileName.lastIndexOf("."),finalFileName.length());
         if("1".equals(uploadMode)){
             OutputStream out = null;
             InputStream in = null;
@@ -333,7 +345,7 @@ public class SftpClientUtil {
                     downLoadFilePath += "/";
                 }
                 //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
-                fileOut = new FileOutputStream(downLoadFilePath + downloadFile);
+                fileOut = new FileOutputStream(downLoadFilePath + headName + System.currentTimeMillis() + endName);
                 BufferedOutputStream bos = new BufferedOutputStream(fileOut);
                 byte[] buf = new byte[4096];
                 int length = bis.read(buf);
@@ -364,7 +376,7 @@ public class SftpClientUtil {
                     //将文件读入文件流
                     InputStream inStream = new FileInputStream(fileurl);
 
-                    File file = new File(downLoadFilePath,finalFileName);
+                    File file = new File(downLoadFilePath,headName + System.currentTimeMillis() + endName);
                     //将下载保存到文件。
                     FileOutputStream out = new FileOutputStream(file);
                     Streams.copy(inStream, out, true);
@@ -377,7 +389,7 @@ public class SftpClientUtil {
 
                     BufferedInputStream bis = new BufferedInputStream(inputStream);
                     //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
-                    FileOutputStream  fileOut = new FileOutputStream(downLoadFilePath + "/" + finalFileName);
+                    FileOutputStream  fileOut = new FileOutputStream(downLoadFilePath + "/" + headName + System.currentTimeMillis() + endName);
                     BufferedOutputStream bos = new BufferedOutputStream(fileOut);
 
                     byte[] buf = new byte[4096];