瀏覽代碼

代码重构

tom.xu@informa.com 2 年之前
父節點
當前提交
32176932c7

+ 12 - 0
pom.xml

@@ -116,6 +116,18 @@
             <scope>system</scope>
             <systemPath>${basedir}/src/main/resources/libs/antlr-2.7.4.jar</systemPath>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.4.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+            <version>2.5.0</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 98 - 0
src/main/java/ieven/server/webapp/api/TestController.java

@@ -0,0 +1,98 @@
+package ieven.server.webapp.api;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.poi.excel.ExcelReader;
+import cn.hutool.poi.excel.ExcelUtil;
+import cn.hutool.poi.excel.ExcelWriter;
+import ieven.server.webapp.service.FileUploadService;
+import ieven.server.webapp.util.HttpClientUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+@Controller
+public class TestController {
+
+  @Autowired FileUploadService fileUploadService;
+
+  public static List<String> getImgSrc(String htmlStr) {
+    if (htmlStr == null) {
+      return null;
+    }
+    String img = "";
+    Pattern p_image;
+    Matcher m_image;
+    List<String> pics = new ArrayList<String>();
+    String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
+    p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
+    m_image = p_image.matcher(htmlStr);
+    while (m_image.find()) {
+      img = img + "," + m_image.group();
+      Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
+      while (m.find()) {
+        pics.add(m.group(1));
+      }
+    }
+    return pics;
+  }
+
+  public static void main(String[] args) {
+    String s =
+        "<p><img src=\"https://img.cbmeonline.com/2021/11/05/82fca5509ca80ef9fd173d2f412d05b4.jpg?imageView2/2/w/750\"/></p>";
+    List<String> imgSrc = getImgSrc(s);
+    for (String s1 : imgSrc) {
+      if (s1.contains("?")) {
+        s1 = s1.substring(0, s1.indexOf("?"));
+      }
+      System.out.println(s1);
+      System.out.println(FileUtil.getName(s1));
+    }
+  }
+
+  @RequestMapping("/test")
+  public String brandList() throws IOException {
+    File file = new File("/home/product.xls");
+    ExcelReader excelReader = ExcelUtil.getReader(file);
+    List<List<Object>> lines = excelReader.read(0, excelReader.getRowCount());
+    for (List<Object> list : lines) {
+      try {
+        String id = (String) list.get(0);
+        System.out.println(id);
+        String content = (String) list.get(1);
+        List<String> imgs = getImgSrc(content);
+        for (String img : imgs) {
+          if (img.contains("?")) {
+            img = img.substring(0, img.indexOf("?"));
+          }
+          String name = FileUtil.getName(img);
+          HttpClientUtils.downloadImage(img, "/home/product/" + name);
+          File file1 = new File("/home/product/" + name);
+          if (file1.exists()) {
+            String s = fileUploadService.fileUpload(new FileInputStream(file1), name);
+            content = content.replace(img, s);
+          }
+        }
+        list.add(2, content);
+      } catch (Exception exception) {
+        ExcelWriter writer = ExcelUtil.getWriter("/home/product2.xls");
+        writer.write(lines);
+        writer.flush();
+        writer.close();
+        exception.printStackTrace();
+      }
+    }
+    ExcelWriter writer = ExcelUtil.getWriter("/home/product2.xls");
+    writer.write(lines);
+    writer.flush();
+    writer.close();
+    return "";
+  }
+}

+ 152 - 0
src/main/java/ieven/server/webapp/service/FileUploadService.java

@@ -0,0 +1,152 @@
+package ieven.server.webapp.service;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.RandomUtil;
+import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.model.ObjectMetadata;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.PostConstruct;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+@Service
+@Slf4j
+public class FileUploadService {
+
+  private String accessId = "LTAI5t5ZTT2hLkQ63ZCQQdU3";
+
+  private String secret = "Qimutfrsi0EvMuPMLTwhpEesVgKuW8";
+
+  private String bucketName = "imcsfiles";
+
+  private String endPoint = "oss-cn-shanghai.aliyuncs.com";
+
+  private String ossWebsite = "https://image.cbmexpo.com";
+
+  private OSSClient ossClient;
+
+  public static final String getContentType(String fileName) {
+    String FilenameExtension = fileName.substring(fileName.lastIndexOf("."));
+    if (FilenameExtension.equalsIgnoreCase(".bmp")) {
+      return "application/x-bmp";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".gif")) {
+      return "image/gif";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".jpeg")
+        || FilenameExtension.equalsIgnoreCase(".jpg")
+        || FilenameExtension.equalsIgnoreCase(".png")) {
+      return "image/jpeg";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".html")) {
+      return "text/html";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".txt")) {
+      return "text/plain";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".vsd")) {
+      return "application/vnd.visio";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".pptx") || FilenameExtension.equalsIgnoreCase(".ppt")) {
+      return "application/vnd.ms-powerpoint";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".docx") || FilenameExtension.equalsIgnoreCase(".doc")) {
+      return "application/msword";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".xla")
+        || FilenameExtension.equalsIgnoreCase(".xlc")
+        || FilenameExtension.equalsIgnoreCase(".xlm")
+        || FilenameExtension.equalsIgnoreCase(".xls")
+        || FilenameExtension.equalsIgnoreCase(".xlt")
+        || FilenameExtension.equalsIgnoreCase(".xlw")) {
+      return "application/vnd.ms-excel";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".xml")) {
+      return "text/xml";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".pdf")) {
+      return "application/pdf";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".zip")) {
+      return "application/zip";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".tar")) {
+      return "application/x-tar";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".avi")) {
+      return "video/avi";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".mp4")) {
+      return "video/mpeg4";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".mp3")) {
+      return "audio/mp3";
+    }
+    if (FilenameExtension.equalsIgnoreCase(".mp2")) {
+      return "audio/mp2";
+    }
+    return "application/octet-stream";
+  }
+
+  @PostConstruct
+  public void initOssClient() {
+    ossClient = new OSSClient(endPoint, accessId, secret);
+  }
+
+  public void test() throws IOException {
+    List<File> loopFiles = FileUtil.loopFiles("F:\\归档");
+    for (File file : loopFiles) {
+      String fileName = file.getAbsolutePath().replace("F:\\归档", "").replace("\\", "/");
+      ObjectMetadata objectMetadata = new ObjectMetadata();
+      FileInputStream fileInputStream = new FileInputStream(file);
+      objectMetadata.setContentLength(fileInputStream.available());
+      String strContentType = getContentType(fileName);
+      objectMetadata.setContentType(strContentType);
+      ossClient.putObject(bucketName, "mm-cbme" + fileName, fileInputStream, objectMetadata);
+      System.out.println(ossWebsite + "/" + "mm-cbme" + fileName);
+    }
+  }
+
+  public String fileUpload(InputStream inputStream, String fileName) throws IOException {
+    String extName = FileUtil.extName(fileName);
+    String fileName2 = DateUtil.today() + "/" + RandomUtil.randomStringUpper(10) + "." + extName;
+    ObjectMetadata objectMetadata = new ObjectMetadata();
+    objectMetadata.setContentLength(inputStream.available());
+    String strContentType = getContentType(fileName);
+    objectMetadata.setContentType(strContentType);
+    ossClient.putObject(bucketName, "mm-cbme/" + fileName2, inputStream, objectMetadata);
+    return ossWebsite + "/" + "mm-cbme/" + fileName2;
+  }
+
+  public String fileUploadWithOriginName(File file, String fileName) throws IOException {
+    FileInputStream fileInputStream = new FileInputStream(file);
+    ObjectMetadata objectMetadata = new ObjectMetadata();
+    objectMetadata.setContentLength(fileInputStream.available());
+    String strContentType = getContentType(fileName);
+    objectMetadata.setContentType(strContentType);
+    ossClient.putObject(bucketName, "mm-cbme/" + fileName, fileInputStream, objectMetadata);
+    return ossWebsite + "/" + "mm-cbme/" + fileName;
+  }
+
+  public String fileUpload(MultipartFile file, String fileName) throws IOException {
+    if (StringUtils.isBlank(fileName)) {
+      fileName = file.getOriginalFilename();
+    }
+    String extName = FileUtil.extName(fileName);
+    String fileName2 = DateUtil.today() + "/" + RandomUtil.randomStringUpper(10) + "." + extName;
+    ObjectMetadata objectMetadata = new ObjectMetadata();
+    objectMetadata.setContentLength(file.getInputStream().available());
+    String strContentType = getContentType(fileName);
+    objectMetadata.setContentType(strContentType);
+    ossClient.putObject(bucketName, "mm-cbme/" + fileName2, file.getInputStream(), objectMetadata);
+    return ossWebsite + "/" + "mm-cbme/" + fileName2 + "?x-oss-process=image/auto-orient,1";
+  }
+}

+ 371 - 0
src/main/java/ieven/server/webapp/util/HttpClientUtils.java

@@ -0,0 +1,371 @@
+package ieven.server.webapp.util;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.TrustStrategy;
+import org.apache.http.entity.StringEntity;
+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.SSLContextBuilder;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.imageio.stream.FileImageOutputStream;
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.security.GeneralSecurityException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * HTTP客户端工具类(支持HTTPS)
+ *
+ * @author ThinkGem
+ * @version 2017-3-27
+ */
+public class HttpClientUtils {
+
+  private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
+
+  /**
+   * http的get请求
+   *
+   * @param url
+   */
+  public static String get(String url) {
+    return get(url, "UTF-8");
+  }
+
+  /**
+   * http的get请求
+   *
+   * @param url
+   */
+  public static String get(String url, String charset) {
+    HttpGet httpGet = new HttpGet(url);
+    return executeRequest(httpGet, charset);
+  }
+
+  public static String getWithHeadersAndQuerys(
+      String url, Map<String, String> headers, Map<String, String> querys)
+      throws UnsupportedEncodingException {
+    HttpGet httpGet = new HttpGet(buildUrl(url, null, querys));
+    return executeRequest(httpGet, headers);
+  }
+
+  /**
+   * http的get请求,增加异步请求头参数
+   *
+   * @param url
+   */
+  public static String ajaxGet(String url) {
+    return ajaxGet(url, "UTF-8");
+  }
+
+  /**
+   * http的get请求,增加异步请求头参数
+   *
+   * @param url
+   */
+  public static String ajaxGet(String url, String charset) {
+    HttpGet httpGet = new HttpGet(url);
+    httpGet.setHeader("X-Requested-With", "XMLHttpRequest");
+    return executeRequest(httpGet, charset);
+  }
+
+  /** http的post请求,传递map格式参数 */
+  public static String post(String url, Map<String, String> dataMap) {
+    return post(url, null, dataMap, "UTF-8");
+  }
+
+  /** http的post请求,传递map格式参数 */
+  public static String post(
+      String url, Map<String, String> headers, Map<String, String> dataMap, String charset) {
+    HttpPost httpPost = new HttpPost(url);
+    if (headers != null) {
+      for (Map.Entry<String, String> entry : headers.entrySet()) {
+        httpPost.setHeader(entry.getKey(), entry.getValue());
+      }
+    }
+    try {
+      if (dataMap != null) {
+        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
+        for (Map.Entry<String, String> entry : dataMap.entrySet()) {
+          nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
+        }
+        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset);
+        formEntity.setContentEncoding(charset);
+        httpPost.setEntity(formEntity);
+      }
+    } catch (UnsupportedEncodingException e) {
+      logger.error(e.getMessage(), e);
+    }
+    return executeRequest(httpPost, charset);
+  }
+
+  /** http的post请求,传递map格式参数 */
+  public static String postHeaders(
+      String url, Map<String, String> headers, String json, String charset) {
+    HttpPost httpPost = new HttpPost(url);
+    for (Map.Entry<String, String> entry : headers.entrySet()) {
+      httpPost.setHeader(entry.getKey(), entry.getValue());
+    }
+    try {
+      if (json != null) {
+        StringEntity requestEntity = new StringEntity(json, "utf-8");
+        requestEntity.setContentEncoding(charset);
+        httpPost.setEntity(requestEntity);
+      }
+    } catch (Exception e) {
+      logger.error(e.getMessage(), e);
+    }
+    return executeRequest(httpPost, charset);
+  }
+
+  /** http的post请求,增加异步请求头参数,传递map格式参数 */
+  public static String ajaxPost(String url, Map<String, String> dataMap) {
+    return ajaxPost(url, dataMap, "UTF-8");
+  }
+
+  /** http的post请求,增加异步请求头参数,传递map格式参数 */
+  public static String ajaxPost(String url, Map<String, String> dataMap, String charset) {
+    HttpPost httpPost = new HttpPost(url);
+    httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
+    try {
+      if (dataMap != null) {
+        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
+        for (Map.Entry<String, String> entry : dataMap.entrySet()) {
+          nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
+        }
+        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset);
+        formEntity.setContentEncoding(charset);
+        httpPost.setEntity(formEntity);
+      }
+    } catch (UnsupportedEncodingException e) {
+      e.printStackTrace();
+    }
+    return executeRequest(httpPost, charset);
+  }
+
+  /** http的post请求,增加异步请求头参数,传递json格式参数 */
+  public static String ajaxPostJson(String url, String jsonString) {
+    return ajaxPostJson(url, jsonString, "UTF-8");
+  }
+
+  /** http的post请求,增加异步请求头参数,传递json格式参数 */
+  public static String ajaxPostJson(String url, String jsonString, String charset) {
+    HttpPost httpPost = new HttpPost(url);
+    httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
+    StringEntity stringEntity = new StringEntity(jsonString, charset); // 解决中文乱码问题
+    stringEntity.setContentEncoding(charset);
+    stringEntity.setContentType("application/json");
+    httpPost.setEntity(stringEntity);
+    return executeRequest(httpPost, charset);
+  }
+
+  /** 执行一个http请求,传递HttpGet或HttpPost参数 */
+  public static String executeRequest(HttpUriRequest httpRequest) {
+    return executeRequest(httpRequest, "UTF-8");
+  }
+
+  public static String executeRequest(HttpUriRequest httpRequest, Map<String, String> headers) {
+    CloseableHttpClient httpclient;
+    if ("https".equals(httpRequest.getURI().getScheme())) {
+      httpclient = createSSLInsecureClient();
+    } else {
+      httpclient = HttpClients.createDefault();
+    }
+    if (headers != null) {
+      for (Map.Entry<String, String> entry : headers.entrySet()) {
+        httpRequest.setHeader(entry.getKey(), entry.getValue());
+      }
+    }
+    String result = "";
+    try {
+      try {
+        CloseableHttpResponse response = httpclient.execute(httpRequest);
+        HttpEntity entity = null;
+        try {
+          entity = response.getEntity();
+          result = EntityUtils.toString(entity, "UTF-8");
+          logger.info("result:" + result);
+        } finally {
+          EntityUtils.consume(entity);
+          response.close();
+        }
+      } finally {
+        httpclient.close();
+      }
+    } catch (IOException ex) {
+      logger.error(ex.getMessage(), ex);
+    }
+    return result;
+  }
+
+  /** 执行一个http请求,传递HttpGet或HttpPost参数 */
+  public static String executeRequest(HttpUriRequest httpRequest, String charset) {
+    CloseableHttpClient httpclient;
+    if ("https".equals(httpRequest.getURI().getScheme())) {
+      httpclient = createSSLInsecureClient();
+    } else {
+      httpclient = HttpClients.createDefault();
+    }
+    String result = "";
+    try {
+      try {
+        CloseableHttpResponse response = httpclient.execute(httpRequest);
+        HttpEntity entity = null;
+        try {
+          entity = response.getEntity();
+          result = EntityUtils.toString(entity, charset);
+          logger.info("result:" + result);
+        } finally {
+          EntityUtils.consume(entity);
+          response.close();
+        }
+      } finally {
+        httpclient.close();
+      }
+    } catch (IOException ex) {
+      logger.error(ex.getMessage(), ex);
+    }
+    return result;
+  }
+
+  /** 创建 SSL连接 */
+  public static CloseableHttpClient createSSLInsecureClient() {
+    try {
+      SSLContext sslContext =
+          new SSLContextBuilder()
+              .loadTrustMaterial(
+                  new TrustStrategy() {
+                    @Override
+                    public boolean isTrusted(X509Certificate[] chain, String authType)
+                        throws CertificateException {
+                      return true;
+                    }
+                  })
+              .build();
+      SSLConnectionSocketFactory sslsf =
+          new SSLConnectionSocketFactory(
+              sslContext,
+              new HostnameVerifier() {
+                @Override
+                public boolean verify(String hostname, SSLSession session) {
+                  return true;
+                }
+              });
+      return HttpClients.custom().setSSLSocketFactory(sslsf).build();
+    } catch (GeneralSecurityException ex) {
+      throw new RuntimeException(ex);
+    }
+  }
+
+  private static String buildUrl(String host, String path, Map<String, String> querys)
+      throws UnsupportedEncodingException {
+    StringBuilder sbUrl = new StringBuilder();
+    sbUrl.append(host);
+    if (!StringUtils.isBlank(path)) {
+      sbUrl.append(path);
+    }
+    if (null != querys) {
+      StringBuilder sbQuery = new StringBuilder();
+      for (Map.Entry<String, String> query : querys.entrySet()) {
+        if (0 < sbQuery.length()) {
+          sbQuery.append("&");
+        }
+        if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
+          sbQuery.append(query.getValue());
+        }
+        if (!StringUtils.isBlank(query.getKey())) {
+          sbQuery.append(query.getKey());
+          if (!StringUtils.isBlank(query.getValue())) {
+            sbQuery.append("=");
+            sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
+          }
+        }
+      }
+      if (0 < sbQuery.length()) {
+        sbUrl.append("?").append(sbQuery);
+      }
+    }
+
+    return sbUrl.toString();
+  }
+
+  public static void downloadImage(String url, String filePath) {
+    try {
+      URL httpUrl = new URL(url);
+      HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
+      conn.setRequestMethod("GET");
+      conn.setConnectTimeout(5 * 1000);
+      InputStream inStream = conn.getInputStream(); // 通过输入流获取图片数据
+      byte[] btImg = readInputStreamByte(inStream); // 得到图片的二进制数据
+      byte2image(btImg, filePath);
+    } catch (Exception e) {
+    }
+  }
+
+  /**
+   * 从输入流中获取数据
+   *
+   * @param inStream 输入流
+   * @return
+   * @throws Exception
+   */
+  public static byte[] readInputStreamByte(InputStream inStream) throws Exception {
+    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+    byte[] buffer = new byte[1024];
+    int len = 0;
+    while ((len = inStream.read(buffer)) != -1) {
+      outStream.write(buffer, 0, len);
+    }
+    inStream.close();
+    return outStream.toByteArray();
+  }
+
+  public static ByteArrayOutputStream readInputStream(InputStream inStream) {
+    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+    try {
+      byte[] buffer = new byte[10240];
+      int len = 0;
+      while ((len = inStream.read(buffer)) != -1) {
+        outStream.write(buffer, 0, len);
+      }
+      inStream.close();
+    } catch (IOException e) {
+      logger.error(e.getMessage(), e);
+    }
+    return outStream;
+  }
+
+  public static void byte2image(byte[] data, String path) {
+    if (data.length < 3 || path.equals("")) {
+      return;
+    }
+    try {
+      FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
+      imageOutput.write(data, 0, data.length);
+      imageOutput.close();
+    } catch (Exception ex) {
+      logger.error(ex.getMessage(), ex);
+    }
+  }
+}