文件压缩(ZIP),分包 上传到第三方系统



前言

文件压缩(ZIP),分包 上传到第三方系统


提示:以下是本篇文章正文内容,下面案例可供参考



一、文件压缩ZIP

方式一:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Description:压缩ZIP
 */
public class ZipUtils {

    private static final int BUFFER_SIZE = 2 * 1024;

    /**
     * 压缩成ZIP 方法1
     *
     * @param srcDir           压缩文件夹路径
     * @param out              压缩文件输出流
     * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
     *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            File sourceFile = new File(srcDir);
            compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 压缩成ZIP 方法2
     *
     * @param srcFiles 需要压缩的文件列表
     * @param out      压缩文件输出流
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[BUFFER_SIZE];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 递归压缩方法
     *
     * @param sourceFile       源文件
     * @param zos              zip输出流
     * @param name             压缩后的名称
     * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
     *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws Exception
     */
    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception {
        byte[] buf = new byte[BUFFER_SIZE];
        if (sourceFile.isFile()) {
            // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy文件到zip输出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 需要保留原来的文件结构时,需要对空文件夹进行处理
                if (KeepDirStructure) {
                    // 空文件夹的处理
                    zos.putNextEntry(new ZipEntry(name + "/"));
                    // 没有文件,不需要文件的copy
                    zos.closeEntry();
                }
            } else {
                for (File file : listFiles) {
                    if (file.getName().endsWith(".zip")) {
                        continue;
                    }
                    // 判断是否需要保留原来的文件结构
                    if (KeepDirStructure) {
                        // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                        // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                        compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), KeepDirStructure);
                    }
                }
            }
        }
    }

    //public static void main(String[] args) throws Exception {
    //    /** 测试压缩方法1  */
    //    FileOutputStream fos1 = new FileOutputStream(new File("D:\\1.2.686772.1.8265.20190108001409346\\1.2.686772.1.8265
    //    .20190108001409346.zip"));
    //    ZipUtils.toZip("D:\\1.2.686772.1.8265.20190108001409346", fos1, true);
    //    ///** 测试压缩方法2  */
    //    //List<File> fileList = new ArrayList<>();
    //    //fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/jar.exe"));
    //    //fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/java.exe"));
    //    //FileOutputStream fos2 = new FileOutputStream(new File("c:/mytest02.zip"));
    //    //ZipUtils.toZip(fileList, fos2);
    //}
}

方式二:

/**
 * 压缩为zip文件  
 * @param srcFilePath
 * @param destFilePath
 */
public static void compress(String srcFilePath, String destFilePath) {

   File src = new File(srcFilePath);
   if (!src.exists()) {
      throw new RuntimeException(srcFilePath + "不存在");
   }
   File zipFile = new File(destFilePath);
   try (
         FileOutputStream fos = new FileOutputStream(zipFile);
         CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
         ZipOutputStream zos = new ZipOutputStream(cos);
   ) {
      String baseDir = "";
      compressbyType(src, zos, baseDir);
   } catch (Exception e) {
   }
}

/**
 * @param src
 * @param zos
 * @param baseDir
 */
private static void compressbyType(File src, ZipOutputStream zos, String baseDir) {
   if (!src.exists()) {
      return;
   }
   if (src.isFile()) {
      compressFile(src, zos, baseDir);
   } else if (src.isDirectory()) {
      compressDir(src, zos, baseDir);
   }
}

/**
 * 压缩文件
 */
private static void compressFile(File file, ZipOutputStream zos, String baseDir) {
   if (!file.exists()) {
      return;
   }
   try (
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
   ) {
      ZipEntry entry = new ZipEntry(baseDir + file.getName());
      zos.putNextEntry(entry);
      int count;
      byte[] buf = new byte[8019];
      while ((count = bis.read(buf)) != -1) {
         zos.write(buf, 0, count);
      }
   } catch (Exception e) {
   }
}

/**
 * 压缩文件夹
 */
private static void compressDir(File dir, ZipOutputStream zos, String baseDir) {
   if (!dir.exists()) {
      return;
   }
   File[] files = dir.listFiles();
   if (files.length == 0) {
      try {
         zos.putNextEntry(new ZipEntry(baseDir + dir.getName()
               + File.separator));
      } catch (IOException e) {
      }
   }
   for (File file : files) {
      compressbyType(file, zos, baseDir + dir.getName() + File.separator);
   }
}



二、HttpClient请求

import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.FormBodyPartBuilder;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.util.Map.Entry;

public class HttpClientUtils
{
   private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
   // 编码格式。发送编码格式统一用UTF-8
   private static final String ENCODING = "UTF-8";
   
   // 设置连接超时时间,单位毫秒。
   private static final int CONNECT_TIMEOUT = 60000;
   
   // 请求获取数据的超时时间(即响应时间),单位毫秒。
   private static final int SOCKET_TIMEOUT = 60000;
   
   /**
    * 发送get请求;不带请求头和请求参数
    * 
    * @param url
    *            请求地址
    * @return
    * @throws Exception
    */
   public static HttpClientResult doGet(String url) throws Exception
   {
      return doGet(url, null, null);
   }
   
   /**
    * 发送get请求;带请求参数
    * 
    * @param url
    *            请求地址
    * @param params
    *            请求参数集合
    * @return
    * @throws Exception
    */
   public static HttpClientResult doGet(String url, Map<String, String> params) throws Exception
   {
      return doGet(url, null, params);
   }
   
   /**
    * 发送get请求;带请求头和请求参数
    * 
    * @param url
    *            请求地址
    * @param headers
    *            请求头集合
    * @param params
    *            请求参数集合
    * @return
    * @throws Exception
    */
   private static HttpClientResult doGet(String url, Map<String, String> headers,
         Map<String, String> params)
   {
      try(CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse httpResponse = null;)
      {

      // 创建访问的地址
      URIBuilder uriBuilder = new URIBuilder(url);
      if (params != null)
      {
         Set<Entry<String, String>> entrySet = params.entrySet();
         for (Entry<String, String> entry : entrySet)
         {
            uriBuilder.setParameter(entry.getKey(), entry.getValue());
         }
      }
      
      // 创建http对象
      HttpGet httpGet = new HttpGet(uriBuilder.build());
      /**
       * setConnectTimeout:设置连接超时时间,单位毫秒。
       * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
       * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
       * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。
       * 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
       */
      RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT).build();
      httpGet.setConfig(requestConfig);
      
      // 设置请求头
      packageHeader(headers, httpGet);

      // 执行请求并获得响应结果
         return getHttpClientResult(httpResponse, httpClient, httpGet);
      }catch (Exception e){
         logger.warn(e.getMessage());
      }
      return new  HttpClientResult();
   }

    public static HttpClientResult doGet(CloseableHttpClient httpClient, String url, Map<String, String> headers,
                                         Map<String, String> params) throws Exception
    {
        // 创建访问的地址
        URIBuilder uriBuilder = new URIBuilder(url);
        if (params != null)
        {
            Set<Entry<String, String>> entrySet = params.entrySet();
            for (Entry<String, String> entry : entrySet)
            {
                uriBuilder.setParameter(entry.getKey(), entry.getValue());
            }
        }

        // 创建http对象
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        /**
         * setConnectTimeout:设置连接超时时间,单位毫秒。
         * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
         * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
         * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。
         * 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
         */
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
                .setSocketTimeout(SOCKET_TIMEOUT).build();
        httpGet.setConfig(requestConfig);

        // 设置请求头
        packageHeader(headers, httpGet);

        // 创建httpResponse对象
        CloseableHttpResponse httpResponse = null;

        try
        {
            // 执行请求并获得响应结果
            return getHttpClientResult(httpResponse, httpClient, httpGet);
        }
        finally
        {
            // 释放资源
            release(httpResponse, httpClient);
        }
    }
   
   /**
    * 发送post请求;不带请求头和请求参数
    * 
    * @param url
    *            请求地址
    * @return
    * @throws Exception
    */
   public static HttpClientResult doPost(String url) throws Exception
   {
      return doPost(url, null, new HashMap<String, String>());
   }
   
   /**
    * 发送post请求;带请求参数
    * 
    * @param url
    *            请求地址
    * @param params
    *            参数集合
    * @return
    * @throws Exception
    */
   public static HttpClientResult doPost(String url, Map<String, String> params) throws Exception
   {
      return doPost(url, null, params);
   }

    /*入参说明
     *
     * param url 请求地址
     * param map 请求的Map<String, Object>数据
     *
     * */
    public static String sendPost(String url, Map<String, Object> map) {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);//设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "UTF-8");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
   
   public static HttpClientResult doPost(CloseableHttpClient httpClient, String url, Map<String, String> params) throws Exception
   {
      return doPost(httpClient, url, null, params);
   }
   
   /**
    * 发送post请求;带请求头和请求参数
    * 
    * @param url
    *            请求地址
    * @param headers
    *            请求头集合
    * @param params
    *            请求参数集合
    * @return
    * @throws Exception
    */
   private static HttpClientResult doPost(String url, Map<String, String> headers,
         Map<String, String> params)
   {
      try(CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse httpResponse = null;){
         // 创建httpClient对象


         // 创建http对象
         HttpPost httpPost = new HttpPost(url);
         /**
          * setConnectTimeout:设置连接超时时间,单位毫秒。
          * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
          * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
          * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。
          * 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
          */
         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
               .setSocketTimeout(SOCKET_TIMEOUT).build();
         httpPost.setConfig(requestConfig);
         // 设置请求头
         /*
          * httpPost.setHeader("Cookie", ""); httpPost.setHeader("Connection",
          * "keep-alive"); httpPost.setHeader("Accept", "application/json");
          * httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
          * httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
          * httpPost.setHeader("User-Agent",
          * "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
          * );
          */
         packageHeader(headers, httpPost);
         // 封装请求参数
         packageParam(params, httpPost);

         // 创建httpResponse对象


            // 执行请求并获得响应结果
         return getHttpClientResult(httpResponse, httpClient, httpPost);

      }catch (Exception e){
         logger.warn(e.getMessage());
      }
      return new HttpClientResult();
   }
   
   public static HttpClientResult doPost(CloseableHttpClient httpClient, String url, Map<String, String> headers,
         Map<String, String> params) throws Exception
   {
      // 创建http对象
      HttpPost httpPost = new HttpPost(url);
      /**
       * setConnectTimeout:设置连接超时时间,单位毫秒。
       * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
       * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
       * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。
       * 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
       */
      RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT).build();
      httpPost.setConfig(requestConfig);
      // 设置请求头
      /*
       * httpPost.setHeader("Cookie", ""); httpPost.setHeader("Connection",
       * "keep-alive"); httpPost.setHeader("Accept", "application/json");
       * httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
       * httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
       * httpPost.setHeader("User-Agent",
       * "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
       * );
       */
      packageHeader(headers, httpPost);
      // 封装请求参数
      packageParam(params, httpPost);
      
      // 创建httpResponse对象
      CloseableHttpResponse httpResponse = null;
      
      try
      {
         // 执行请求并获得响应结果
         return getHttpClientResult(httpResponse, httpClient, httpPost);
      }
      finally
      {
         // 释放资源
         release(httpResponse, httpClient);
      }
   }
   
   public static HttpClientResult doPost(CloseableHttpClient httpClient, String url,
         Map<String, String> headers, String jsonString) throws Exception
   {
      // 创建http对象
      HttpPost httpPost = new HttpPost(url);
      /**
       * setConnectTimeout:设置连接超时时间,单位毫秒。
       * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
       * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
       * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。
       * 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
       */
      RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT).build();
      httpPost.setConfig(requestConfig);
      // 设置请求头
      /*
       * httpPost.setHeader("Cookie", ""); httpPost.setHeader("Connection",
       * "keep-alive"); httpPost.setHeader("Accept", "application/json");
       * httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
       * httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
       * httpPost.setHeader("User-Agent",
       * "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
       * );
       */
      packageHeader(headers, httpPost);
      httpPost.setHeader("Content-Type", "application/json");
      // 封装请求参数
      httpPost.setEntity(new StringEntity(jsonString, ENCODING));
      
      // 创建httpResponse对象
      CloseableHttpResponse httpResponse = null;
      
      try
      {
         // 执行请求并获得响应结果
         return getHttpClientResult(httpResponse, httpClient, httpPost);
      }
      finally
      {
         // 释放资源
         release(httpResponse, httpClient);
      }
   }
   
   public static HttpClientResult doPost(String url, Map<String, String> headers,
         String jsonString)
   {
      try(CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse httpResponse = null;){
         // 创建http对象
         HttpPost httpPost = new HttpPost(url);
         /**
          * setConnectTimeout:设置连接超时时间,单位毫秒。
          * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
          * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
          * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。
          * 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
          */
         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
               .setSocketTimeout(SOCKET_TIMEOUT).build();
         httpPost.setConfig(requestConfig);
         // 设置请求头
         /*
          * httpPost.setHeader("Cookie", ""); httpPost.setHeader("Connection",
          * "keep-alive"); httpPost.setHeader("Accept", "application/json");
          * httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
          * httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
          * httpPost.setHeader("User-Agent",
          * "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
          * );
          */
         packageHeader(headers, httpPost);
         httpPost.setHeader("Content-Type", "application/json");
         // 封装请求参数
         httpPost.setEntity(new StringEntity(jsonString, ENCODING));
         // 执行请求并获得响应结果
         return getHttpClientResult(httpResponse, httpClient, httpPost);

      }catch (SocketTimeoutException es) {
         logger.warn("服务器响应超时 es={}",es.getMessage());
      }catch (ConnectTimeoutException ex) {
         logger.warn("服务器请求超时 ex={}",ex.getMessage());
      }catch (Exception e){
         logger.warn(e.getMessage());
      }
      return new HttpClientResult();
   }


   public static HttpClientResult doPost(String url, HttpEntity entity) {
      return  doPost(url, null, entity);
   }

   public static HttpClientResult doPost(String url, Map<String, String> headers, HttpEntity entity) {
      try(CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse httpResponse = null){
         // 创建http对象
         HttpPost httpPost = new HttpPost(url);
         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
               .setSocketTimeout(SOCKET_TIMEOUT).build();
         httpPost.setConfig(requestConfig);
         // 设置请求头
         httpPost.setHeader("Cookie", "");
         httpPost.setHeader("Connection", "keep-alive");
         httpPost.setHeader("Accept", "*/*");
         httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
         httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
         httpPost.setHeader("User-Agent",
           "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
          );
         packageHeader(headers, httpPost);
         // 封装请求参数
         httpPost.setEntity(entity);
         // 执行请求并获得响应结果
         return getHttpClientResult(httpResponse, httpClient, httpPost);
      }catch (Exception e){
         logger.error(e.getMessage());
      }
      return new HttpClientResult();
   }

   /**
    * 发送post请求;带请求头和请求参数(参数为File)
    *
    * @param url 请求地址
    * @param headers 请求头集合
    * @param fileUrl 请求参数文件
    * @return
    * @throws Exception
    */
   public static HttpClientResult doPostFile(String url, Map<String, String> headers, String fileUrl, String jsonStr, Integer jsonLen) {
      try (CloseableHttpClient httpClient = HttpClients.createDefault();
          CloseableHttpResponse httpResponse = null;) {
         // 创建http对象
         HttpPost httpPost = new HttpPost(url);
         /**
          * setConnectTimeout:设置连接超时时间,单位毫秒。
          * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
          * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
          * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
          */
         RequestConfig requestConfig =
               RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
         httpPost.setConfig(requestConfig);
         // 设置请求头
         /*httpPost.setHeader("Cookie", "");
         httpPost.setHeader("Connection", "keep-alive");
         httpPost.setHeader("Accept", "application/json");
         httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
         httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
         httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325
         .181 Safari/537.36");
         */
         packageHeader(headers, httpPost);

         // 封装请求参数
         packageParamForFile(fileUrl, httpPost, jsonStr, jsonLen);

         try {
            // 执行请求并获得响应结果
            return getHttpClientResult(httpResponse, httpClient, httpPost);
         } finally {
            // 释放资源
            release(httpResponse, httpClient);
         }
      } catch (Exception e) {
         logger.error(e.getMessage());
      }
      return new HttpClientResult();
   }

   /**
    * Description: 封装请求参数(参数为File)
    *
    * @param
    * @param httpMethod
    * @throws UnsupportedEncodingException
    */
   public static void packageParamForFile(String fileUrl, HttpEntityEnclosingRequestBase httpMethod,String jsonStr,Integer jsonLen){
      try {
         FileBody bin = new FileBody(new File(fileUrl));
         StringBody comment = new StringBody("This is comment", ContentType.TEXT_PLAIN);
         // 封装请求参数
         MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create()
               .setCharset(Charset.forName("UTF-8"))
               .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
               .addPart("file", bin)
               .addPart("comment", comment);
         if (StringUtils.isNotEmpty(jsonStr)) {
            FormBodyPart formBodyPart = FormBodyPartBuilder.create().setName("blh").setBody(new StringBody(jsonStr,
                  ContentType.DEFAULT_TEXT)).build();
            multipartEntityBuilder.addPart(formBodyPart);
            }
         if (null !=jsonLen) {
            FormBodyPart formBodyPart2 = FormBodyPartBuilder.create().setName("index").setBody(new StringBody(String.valueOf(jsonLen),
                  ContentType.DEFAULT_TEXT)).build();
            multipartEntityBuilder.addPart(formBodyPart2);
         }
         // 设置到请求的http对象中
         httpMethod.setEntity(multipartEntityBuilder.build());
      } catch (Exception e) {
         logger.error("packageParamForFile={}",e.getMessage());
      }
   }
   
   public static HttpClientResult doPost(String url, Map<String, String> headers,
         String jsonString, Map<String, String> params)
   {
      try(CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse httpResponse = null;){
         // 创建httpClient对象

         // 创建访问的地址
         URIBuilder uriBuilder = new URIBuilder(url);
         if (params != null)
         {
            Set<Entry<String, String>> entrySet = params.entrySet();
            for (Entry<String, String> entry : entrySet)
            {
               uriBuilder.setParameter(entry.getKey(), entry.getValue());
            }
         }

         // 创建http对象
         HttpPost httpPost = new HttpPost(uriBuilder.build());
         /**
          * setConnectTimeout:设置连接超时时间,单位毫秒。
          * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
          * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
          * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。
          * 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
          */
         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
               .setSocketTimeout(SOCKET_TIMEOUT).build();
         httpPost.setConfig(requestConfig);
         // 设置请求头
         /*
          * httpPost.setHeader("Cookie", ""); httpPost.setHeader("Connection",
          * "keep-alive"); httpPost.setHeader("Accept", "application/json");
          * httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
          * httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
          * httpPost.setHeader("User-Agent",
          * "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
          * );
          */
         packageHeader(headers, httpPost);
         httpPost.setHeader("Content-Type", "application/json");
         // 封装请求参数
         httpPost.setEntity(new StringEntity(jsonString, ENCODING));

         // 创建httpResponse对象



         // 执行请求并获得响应结果
         return getHttpClientResult(httpResponse, httpClient, httpPost);
      }catch (Exception e){
         logger.warn(e.getMessage());
      }
      return new HttpClientResult();
   }
   
   /**
    * 发送put请求;不带请求参数
    * 
    * @param url
    *            请求地址
    *
    * @return 请求结果
    */
   public static HttpClientResult doPut(String url) {
      return doPut(url);
   }
   
   /**
    * 发送put请求;带请求参数
    * 
    * @param url
    *            请求地址
    * @param params
    *            参数集合
    * @return
    * @throws Exception
    */
   public static HttpClientResult doPut(String url, Map<String, String> params) throws Exception
   {
      try (CloseableHttpClient httpClient = HttpClients.createDefault();
          CloseableHttpResponse httpResponse = null;){

         HttpPut httpPut = new HttpPut(url);
         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
               .setSocketTimeout(SOCKET_TIMEOUT).build();
         httpPut.setConfig(requestConfig);

         packageParam(params, httpPut);




         return getHttpClientResult(httpResponse, httpClient, httpPut);

      }catch (Exception e){
         logger.warn(e.getMessage());
      }

      return new HttpClientResult();
   }
   
   /**
    * 发送delete请求;不带请求参数
    * 
    * @param url
    *            请求地址
    *            参数集合
    * @return
    * @throws Exception
    */
   public static HttpClientResult doDelete(String url) throws Exception
   {
      CloseableHttpClient httpClient = HttpClients.createDefault();
      HttpDelete httpDelete = new HttpDelete(url);
      RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT).build();
      httpDelete.setConfig(requestConfig);
      
      CloseableHttpResponse httpResponse = null;
      try
      {
         return getHttpClientResult(httpResponse, httpClient, httpDelete);
      }
      finally
      {
         release(httpResponse, httpClient);
      }
   }
   
   /**
    * 发送delete请求;带请求参数
    * 
    * @param url
    *            请求地址
    * @param params
    *            参数集合
    * @return
    * @throws Exception
    */
   public static HttpClientResult doDelete(String url, Map<String, String> params) throws Exception
   {
      if (params == null)
      {
         params = new HashMap<String, String>();
      }
      
      params.put("_method", "delete");
      return doPost(url, params);
   }
   
   /**
    * Description: 封装请求头
    * 
    * @param params
    * @param httpMethod
    */
   public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod)
   {
      // 封装请求头
      if (params != null)
      {
         Set<Entry<String, String>> entrySet = params.entrySet();
         for (Entry<String, String> entry : entrySet)
         {
            // 设置到请求头到HttpRequestBase对象中
            httpMethod.setHeader(entry.getKey(), entry.getValue());
         }
      }
   }
   
   /**
    * Description: 封装请求参数
    * 
    * @param params
    * @param httpMethod
    * @throws UnsupportedEncodingException
    */
   public static void packageParam(Map<String, String> params,
         HttpEntityEnclosingRequestBase httpMethod) throws UnsupportedEncodingException
   {
      // 封装请求参数
      if (params != null)
      {
         List<NameValuePair> nvps = new ArrayList<NameValuePair>();
         Set<Entry<String, String>> entrySet = params.entrySet();
         for (Entry<String, String> entry : entrySet)
         {
            nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
         }
         
         // 设置到请求的http对象中
         httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
      }
   }
   
   /**
    * Description: 获得响应结果
    * 
    * @param httpResponse
    * @param httpClient
    * @param httpMethod
    * @return
    * @throws Exception
    */
   private static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,
         CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception
   {

      if (httpResponse == null){
         httpResponse = httpClient.execute(httpMethod);
      }


      // 获取返回结果
      if (httpResponse != null && httpResponse.getStatusLine() != null)
      {
         String content = "";
         if (httpResponse.getEntity() != null)
         {
            content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
         }
         Header[] headers = httpResponse.getAllHeaders();
         Map<String, String> headerMap = new HashMap<>();
         for (Header header : headers)
         {
            headerMap.put(header.getName(), header.getValue());
         }
         return new HttpClientResult(httpResponse.getStatusLine()
               .getStatusCode(), content, headerMap);
      }
      return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
   }
   
   /**
    * Description: 释放资源
    * 
    * @param httpResponse
    * @param httpClient
    * @throws IOException
    */
   public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient)
         throws IOException
   {
      // 释放资源
      if (httpResponse != null)
      {
         httpResponse.close();
      }
      if (httpClient != null)
      {
         httpClient.close();
      }
   }
   
   public static CloseableHttpClient getIgnoeSSLClient() throws Exception
   {
      SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy()
      {
         @Override
         public boolean isTrusted(X509Certificate[] x509Certificates, String s)
               throws CertificateException
         {
            return true;
         }
         
      }).build();
      
      // 创建httpClient
      CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext)
            .setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
      return client;
   }

}

三、完整实现过程

        1.创建定时任务 执行文件压缩并且上传到第三方系统

                1.1 压缩为ZIP包(按照10M拆分为小包  在上传)

                1.2(直接按照文件名压缩,返回文件路径给第三方,第三方系统自己取压缩包)

        


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;


@Component
public class AiScheduledTask {
    private final AiStudyProcessTaskService aiStudyProcessTaskService;
    private static final Logger logger = LoggerFactory.getLogger(AiStudyUploadTask.class);
    private static final String PATH_DELIMITER = File.separator;

    private static final String FILE_UPLOAD = "";
    private static final String FILE_UPLOAD_FINISH = "";

    @Value(("${xxx.url}"))
    private String url;

    @Value(("${xxx.thirdId}"))
    private int thirdId;

    @Value(("${xxx.charge}"))
    private boolean charge;


    @Scheduled(cron = "${aiIntegration.cron}")
    public void compressUploadStudy() {
        // 获取定时任务表中所有待压缩/上传任务
        List<Map<String, Object>> tasks = aiStudyProcessTaskService.getAllTasks();
        for (Map<String, Object> t : tasks) {
            try {
                1.压缩
                List<String> seriesZip = compressBySeries(t);
                2.上传
                uploadSeriesZip(seriesZip, t);
            } catch (Exception ex) {
               
            }
        }
    }

    /**
     * 上传分序列压缩包  
     *
     * @param seriesZip
     * @param taskMap
     * @return
     * @throws Exception
     */
    public JSONObject uploadSeriesZip(List<String> seriesZip, Map<String, Object> taskMap) {
        if (CollectionUtils.isEmpty(seriesZip)) {
            logger.info("参数不正确 seriesZip={}", seriesZip);
            return ResultUtils.errorResult("参数不正确 seriesZip");
        }

        String token = rxDiagService.getToken();
        String url = url + FILE_UPLOAD;
        Map<String, String> headerMap = Maps.newHashMap();
        if (StringUtils.isEmpty(token)) {
            return ResultUtils.errorResult("获取RXinAI token失败");
        }
        token = "Bearer " + token;
        headerMap.put("Authorization", token);
        long start = System.currentTimeMillis();
        for (int i = 0; i < seriesZip.size(); i++) {
            //文件
            String fileUrl = seriesZip.get(i);
            //分片序号
            int index = i;
            HttpClientResult httpResult = HttpClientUtils.doPostFile(url, headerMap, fileUrl, blh, index);
            logger.info("RXinAI 请求结果httpResult={}", httpResult);
            String content = httpResult.getContent();
            if (StringUtils.isEmpty(content)) {
                return ResultUtils.errorResult("文件上传接口异常");
            }
            JSONObject jsonObject = JSON.parseObject(content);
            if (jsonObject.getInteger("code") != HttpStatus.OK.value()) {
                String msg = ParamsUtils.getStrParam(jsonObject.getString("message"), null);
                logger.error("文件上传接口异常={}", msg);
                return ResultUtils.errorResult("文件上传接口异常msg={}" + msg);
            } else {
                String message = ParamsUtils.getStrParam(jsonObject.getString("message"), null);
                boolean success = ParamsUtils.getBooleanParam(jsonObject.getBoolean("success"), Boolean.TRUE);
                if (success) {
                    isSucceeded = Boolean.TRUE;
                    logger.info("上传成功,message={},success={}", message, success);
                }
            }
        }
        long end = System.currentTimeMillis();
        logger.info("上传完成,耗时:" + (end - start) + " ms");
        if (isSucceeded) {
            //自己的业务逻辑
        }
        return null;
    }


    /**
     * 压缩
     *
     */
    public static List<String> compressBySeries(Map<String, Object> taskMap) throws IOException {
        List<String> list = new ArrayList<>();
        Map<String, String> FileMap = new HashMap<>();
        String aiPlatformNo = "";
        if (StringUtil.isEmpty(aiPlatformNo)) {
            return null;
        }
        String studyFilePath = = "";
        if (StringUtils.isEmpty(studyFilePath)) {
            return null;
        }
        File file = new File(studyFilePath);
        //方式二(直接遍历文件夹  压缩为ZIP包)
        if (AIPlatformEnum.TI_SU.getValue().equals(aiPlatformNo)) {
            List<File> fileList = FileUtils.getFiles(file);
            ArrayList<String> iconNameList = new ArrayList<>();
            for (int i = 0; i < fileList.size(); i++) {
                String curPath = fileList.get(i).getPath();
                iconNameList.add(fileList.get(i).getName());
                FileMap.put(fileList.get(i).getName(), curPath);
            }
            logger.info("FileMap={},\r\n,iconNameList={}", FileMap, iconNameList);
            //压缩为zip包
            for (int i = 0; i < iconNameList.size(); i++) {
                String fileUrl = FileMap.get(iconNameList.get(i));
                String zipFileName = studyFilePath + PATH_DELIMITER + iconNameList.get(i) + ".zip";
                FileZipUtil.compress(fileUrl, zipFileName);
                // 返回压缩包全路径名
                list.add(zipFileName);
            }
            logger.info("压缩为ZIP包完成list={}", list);
            return list;
        }
        //方式一(压缩为ZIP包  在按照10M拆包  发送给第三方系统)
        if (AIPlatformEnum.RUI_XIN.getValue().equals(aiPlatformNo)) {
            studyFilePath = studyFilePath.trim();
            String zipFileName = studyFilePath.substring(studyFilePath.lastIndexOf("/") + 1);
            String zipFilePath = studyFilePath + PATH_DELIMITER + zipFileName + ".zip";
            logger.info("压缩为ZIP包路径zipFilePath={}", zipFilePath);
            FileOutputStream fos1 = new FileOutputStream(new File(zipFilePath));
            ZipUtils.toZip(studyFilePath, fos1, true);
            List<String> splitList = FileUtils.split(zipFilePath, FILENAMES, studyFilePath);
            logger.info("压缩为ZIP包完成:splitList={}", splitList);
            return splitList;
        }
        return null;
    }
}

工具类:

        遍历文件:

public static List<File> getFiles(File file) {
    List<File> resultFileName = new ArrayList<>();
    File[] files = file.listFiles();
    // 判断目录下是不是空的
    if (files == null) {
        return resultFileName;
    }
    for (File f : files) {
        if (f.getName().endsWith(".zip")) {
            continue;
        }
        // 判断是否文件夹
        if (f.isDirectory()) {
            resultFileName.add(new File(f.getPath()));
            //递归, 调用自身,查找子目录
            getFiles(f);
        } else {
            resultFileName.add(new File(f.getPath()));
        }
    }
    return resultFileName;
}
/**
 * 删除ZIP压缩文件
 *
 * @param file 需要删除的文件路径
 * @return true:删除成功 false:删除失败
 */
public static boolean deleteZipFile(String file) {
    try {
        File[] files = new File(file).listFiles();
        for (File f : files) {
            if (f.getName().endsWith(".zip")) {
                org.apache.commons.io.FileUtils.forceDelete(f);
            }
        }
        return true;
    } catch (IOException ex) {
        logger.warn(String.format("删除ZIP文件出错: %s", ex.getMessage()));
    }
    return false;
}

                

        




总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值