java 通过接口下载excel并转实体类工具类

时间:2024-06-09 07:16:16
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.support.ExcelTypeEnum; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.ssl.SSLContextBuilder; import org.springframework.lang.Nullable; import javax.net.ssl.SSLContext; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * @author dume * @ClassName HttpClientDownload * @description: TODO * @date 2023年11月14日 * @version: 1.0 */ public class HttpClientDownload { public static <T> List<T> doGetDownload(String url, boolean isHttps, String authorization,Class zclass) { InputStream inputStream = null; //1.创建HttpClient对象 org.apache.http.impl.client.CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; List<T> list = new ArrayList<>(); try { //这里我加了一个是否需要创建一个https连接的判断 if (isHttps) { //配置https请求的一些参数 SSLContext sslContext = SSLContextBuilder.create().useProtocol(SSLConnectionSocketFactory.SSL).loadTrustMaterial((x, y) -> true).build(); RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(60000).build(); httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).setSSLContext(sslContext).setSSLHostnameVerifier((x, y) -> true).build(); } else { httpClient = HttpClientBuilder.create().build(); } //2.生成get请求对象,并设置请求头信息 HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Authorization", authorization); httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); //3.执行请求 response = httpClient.execute(httpGet); //4.处理响应信息 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); inputStream = entity.getContent(); list = readExcelConvertObjectList(inputStream,ExcelTypeEnum.XLS,zclass); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (httpClient != null) { httpClient.close(); } if (response != null) { response.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return list; } /** * 使用Class来读取Excel * * @param inputStream Excel的输入流 * @param excelTypeEnum Excel的格式(XLS或XLSX) * @return 返回 ClassList 的列表 */ public static <T> List<T> readExcelConvertObjectList(InputStream inputStream, ExcelTypeEnum excelTypeEnum, Class<T> classT) { return readExcelConvertObjectList(inputStream, excelTypeEnum, 0,1, classT); } /** * 读取excel数据到数据对象 * * @param inputStream 文件流 * @param excelTypeEnum 文件类型Excel的格式(XLS或XLSX) * @param headLineNum 开始读取数据的行 * @param classT 转为对象的CLASS * @param <T> * @return */ public static <T> List<T> readExcelConvertObjectList(InputStream inputStream, ExcelTypeEnum excelTypeEnum, @Nullable Integer sheet, @Nullable Integer headLineNum, Class<T> classT) { if (headLineNum == null) { headLineNum = 1; } if (sheet == null) { sheet = 0; } return EasyExcel.read(inputStream).excelType(excelTypeEnum) .head(classT) .sheet(sheet) .headRowNumber(headLineNum) .doReadSync(); } }