java远程文件url怎么转为输入流
发表于:2024-10-14 作者:千家信息网编辑
千家信息网最后更新 2024年10月14日,这篇文章主要介绍"java远程文件url怎么转为输入流",在日常操作中,相信很多人在java远程文件url怎么转为输入流问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"j
千家信息网最后更新 2024年10月14日java远程文件url怎么转为输入流
这篇文章主要介绍"java远程文件url怎么转为输入流",在日常操作中,相信很多人在java远程文件url怎么转为输入流问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"java远程文件url怎么转为输入流"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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
public static String getCellFormatValue(Cell cell) { cell.setCellType(CellType.STRING); return cell.getStringCellValue();}
根据URL网址获取输入流
方法一
//文件访问路径String url = "";InputStream intstream = new URL(url).openStream();
方法二
public InputStream getInputStreamByUrl(String strUrl) { HttpURLConnection conn = null; try { URL url = new URL(strUrl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(20 * 1000); final ByteArrayOutputStream output = new ByteArrayOutputStream(); IOUtils.copy(conn.getInputStream(), output); return new ByteArrayInputStream(output.toByteArray()); } catch (Exception e) { logger.error("getInputStreamByUrl 异常,exception is {}", e); } finally { try { if (conn != null) { conn.disconnect(); } } catch (Exception e) { } } return null; }
到此,关于"java远程文件url怎么转为输入流"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
文件
输入
学习
方法
表格
内容
数据
更多
顺序
帮助
实用
接下来
一行
信息
头部
姓名
对象
文章
标题
理论
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
源代码 域名 服务器
南邮数据库基础实验三
服务器流量收费吗
天擎网络安全宣传周
学校网络安全机构
滴滴事件后网络安全板块
计算机网络技术包含哪些内容
浪潮英信服务器维修公司
大连沃嘉科技软件开发
内网使用域名访问服务器
成都crm软件开发平台
江阴软件开发五星服务
网络技术高明漱粉
云服务器应用管理
如何检查本地数据库端口通不通
数据库全部注释快捷键
关于系统建设网络安全管理
数据库开发笔试题及答案
应用软件开发大脑壳
上海市服务器机房改造
广西党建设计软件开发系统
软件开发英语几级
网络技术部对企业的重要性
c 怎么和数据库连接起来
软件开发运行原理
如何检查本地数据库端口通不通
廊坊市驻梦网络技术服务有限公司
庆阳电信杯网络安全大赛视频
app软件开发用什么程序编写
图书管理系统数据库结构图