java 远程文件url 转为输入流

URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();

 

public static AjaxModel parseExcelForInfo(InputStream inputStream, String fileName, int taskId) {
    try {
        //创建workbook对象
        Workbook workbook = null;
        if (fileName.contains(".xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else if (fileName.contains(".xls")) {
            workbook = new HSSFWorkbook(inputStream);
        } else {
            return AjaxModel.failed(-1, "文件类型不正确");
        }
        //获取第一个sheet表
        Sheet sheetAt = workbook.getSheetAt(0);
        if (sheetAt != null) {
            // TODO 校验excel头
            Row headRow = sheetAt.getRow(0);
            for (int i = 0; i < BusinessSettlementConstants.TEMPLATE_COULMN.length; i++) {
                if (!FileUtil.getCellFormatValue(headRow.getCell(i)).trim().equals(BusinessSettlementConstants.TEMPLATE_COULMN[i])) {
                    LOGGER.info("parseExcelForInfo excel头部信息顺序不正确,getCellFormatValue(headRow.getCell(i)):{}," +
                                    "BusinessSettlementConstants.TEMPLATE_COULMN[i]:{},taskId:{}", FileUtil.getCellFormatValue(headRow.getCell(i)),
                            BusinessSettlementConstants.TEMPLATE_COULMN[i], taskId);
                    return AjaxModel.failed("excel标题头顺序不正确:" + FileUtil.getCellFormatValue(headRow.getCell(i)));
                }
            }
            int startRowNum = sheetAt.getFirstRowNum() + 1;
            int lastRowNum = sheetAt.getLastRowNum();

            LOGGER.info("解析excel开始taskId:{},从【{}】行开始,到第【{}】行结束", taskId, startRowNum, lastRowNum);
            List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
            for (int rowNum = startRowNum; rowNum <= lastRowNum; rowNum++) {
                // 每一行数据
                Row row = sheetAt.getRow(rowNum);
                Map<String, String> map = new HashMap<>();
                LOGGER.info("parseExcelForInfo row:{}", row);
                if (row != null && row.getCell(0) != null && StringUtils.isNotEmpty(row.getCell(0).getStringCellValue()) && row.getCell(2) != null && row.getCell(4) != null) {
                    LOGGER.info("parseExcelForInfo row:{},cell:{}", row, row.getCell(0));
                    // 姓名
                    map.put("userName", FileUtil.getCellFormatValue(row.getCell(0)));
                    
                    dataList.add(map);
                }
            }
            LOGGER.info("--------------解析完成 dataList:{}", dataList);
            if (dataList.size() <= 0) {
                return AjaxModel.failed(-1, "解析表格数据为空");
            }
            AjaxModel success = AjaxModel.success();
            success.getData().put("dataList", dataList);
            return success;
        } else {
            LOGGER.info("sheet内容为空");
            return AjaxModel.failed(-1, "表格内容为空");
        }
    } catch (Exception e) {
        LOGGER.error("parseExcelForInfo 解析异常", e);
    }
    return AjaxModel.failed(-1, "解析表格异常");
}

 

public static String getCellFormatValue(Cell cell) {
    cell.setCellType(CellType.STRING);
    return cell.getStringCellValue();
}
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 Java 的 `URL` 类和 `HttpsURLConnection` 类来实现将 HTTPS URL 换为输入。以下是一个示例代码: ``` URL url = new URL("https://www.example.com"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); InputStream inputStream = conn.getInputStream(); ``` 需要注意的是,如果访问的是https 必须需要在安装证书 ### 回答2: 在Java中,可以通过使用标准库的`java.net`包中的类来将HTTPS URL换为输入。以下是一个简单的示例代码: ``` import java.io.*; import java.net.*; public class Main { public static void main(String[] args) { try { // 创建HTTPS URL对象 URL url = new URL("https://example.com"); // 打开HTTPS连接 HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); // 获取输入 InputStream inputStream = conn.getInputStream(); // 将输入读取为字符串 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder content = new StringBuilder(); while ((line = reader.readLine()) != null) { content.append(line); } reader.close(); // 输出读取到的字符串 System.out.println(content.toString()); // 关闭HTTPS连接 conn.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在代码中,首先创建了一个`URL`对象,将目标URL传递给它。然后,通过调用`openConnection()`方法,打开一个`HttpsURLConnection`对象,用于与目标URL建立HTTPS连接。接下来,通过调用`getInputStream()`方法,获取HTTPS连接的输入。将输入包装在`BufferedReader`中,以便逐行读取内容。最后,使用`StringBuilder`将读取到的内容串联起来,并输出到控制台上。 ### 回答3: 在Java中,将HTTPS URL换为输入的方式可以使用JavaURLConnection类。以下是换的步骤: 1. 导入相关的Java包和类: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; ``` 2. 创建一个HTTPS URL对象: ```java URL url = new URL("https://example.com"); ``` 3. 打开URL连接: ```java URLConnection connection = url.openConnection(); ``` 4. 若连接的是HTTPS类型的URL,需要验证证书,可以使用下面的代码来忽略证书验证: ```java HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; httpsConnection.setHostnameVerifier((hostname, session) -> true); ``` 5. 获取输入,从URL中读取数据: ```java InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { // 处理读取到的数据 System.out.println(line); } reader.close(); ``` 6. 最后,记得在处理完输入后关闭相关的资源。 这样,我们就将一个HTTPS URL换为了输入,并可以通过输入读取URL内容。 以上是使用Java将HTTPS URL换为输入的简单示例。请注意,在实际开发中可能还需要处理异常、设置请求头、设置连接超时时间等操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值