这篇文章主要介绍“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<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();
}
//文件访问路径
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怎么转为输入流”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。