java 遠(yuǎn)程文件url如何轉(zhuǎn)為輸入流
更新時(shí)間:2021年10月19日 09:15:16 作者:指尖擋不住流年
這篇文章主要介紹了java 遠(yuǎn)程文件url如何轉(zhuǎn)為輸入流方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
java 遠(yuǎn)程文件url 轉(zhuǎn)為輸入流
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設(shè)置超時(shí)間為3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403錯(cuò)誤
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 {
//創(chuàng)建workbook對(duì)象
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, "文件類型不正確");
}
//獲取第一個(gè)sheet表
Sheet sheetAt = workbook.getSheetAt(0);
if (sheetAt != null) {
// TODO 校驗(yàn)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標(biāo)題頭順序不正確:" + FileUtil.getCellFormatValue(headRow.getCell(i)));
}
}
int startRowNum = sheetAt.getFirstRowNum() + 1;
int lastRowNum = sheetAt.getLastRowNum();
LOGGER.info("解析excel開始taskId:{},從【{}】行開始,到第【{}】行結(jié)束", taskId, startRowNum, lastRowNum);
List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
for (int rowNum = startRowNum; rowNum <= lastRowNum; rowNum++) {
// 每一行數(shù)據(jù)
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, "解析表格數(shù)據(jù)為空");
}
AjaxModel success = AjaxModel.success();
success.getData().put("dataList", dataList);
return success;
} else {
LOGGER.info("sheet內(nèi)容為空");
return AjaxModel.failed(-1, "表格內(nèi)容為空");
}
} catch (Exception e) {
LOGGER.error("parseExcelForInfo 解析異常", e);
}
return AjaxModel.failed(-1, "解析表格異常");
}
public static String getCellFormatValue(Cell cell) {
cell.setCellType(CellType.STRING);
return cell.getStringCellValue();
}
根據(jù)URL網(wǎng)址獲取輸入流
方法一
//文件訪問(wèn)路徑 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;
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Cloud?Studio構(gòu)建SpringSecurity權(quán)限框架(騰訊云?Cloud?Studio?實(shí)戰(zhàn)訓(xùn)練
隨著云計(jì)算技術(shù)的成熟和普及,傳統(tǒng)編程能力和資源以云服務(wù)的形式開放出來(lái),從中間件、數(shù)據(jù)庫(kù)等水平能力服務(wù)組件到人臉識(shí)別、鑒權(quán)服務(wù)等基本業(yè)務(wù)服務(wù)組件很容易的在云端獲取,本文介紹使用Cloud?Studio構(gòu)建SpringSecurity權(quán)限框架的相關(guān)知識(shí),感興趣的朋友一起看看吧2023-08-08
springboot項(xiàng)目打包成jar包的圖文教程
有時(shí)候我們會(huì)用IDEA來(lái)開發(fā)一些小工具,需要打成可運(yùn)行的JAR包,這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目打包成jar包的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Java中synchronized關(guān)鍵字修飾方法同步的用法詳解
synchronized可以用來(lái)同步靜態(tài)和非靜態(tài)方法,下面就具體來(lái)看一下Java中synchronized關(guān)鍵字修飾方法同步的用法詳解:2016-06-06
SSH框架網(wǎng)上商城項(xiàng)目第12戰(zhàn)之添加和更新商品功能
這篇文章主要介紹了SSH框架網(wǎng)上商城項(xiàng)目第12戰(zhàn)之添加和更新商品功能的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-06-06
Jenkins自動(dòng)構(gòu)建部署項(xiàng)目到遠(yuǎn)程服務(wù)器上的方法步驟
這篇文章主要介紹了Jenkins自動(dòng)構(gòu)建部署項(xiàng)目到遠(yuǎn)程服務(wù)器上的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java設(shè)計(jì)模式中裝飾者模式應(yīng)用詳解
裝飾者模式:在不改變?cè)袑?duì)象的基礎(chǔ)之上,動(dòng)態(tài)的將功能附加到對(duì)象上,提供了繼承更有彈性的替代方案,也體現(xiàn)了開閉原則。本文將通過(guò)示例詳細(xì)講解一下裝飾者模式,需要的可以參考一下2022-11-11

