您好,登錄后才能下訂單哦!
這篇文章主要介紹“java遠(yuǎn)程文件url怎么轉(zhuǎn)為輸入流”,在日常操作中,相信很多人在java遠(yuǎn)程文件url怎么轉(zhuǎn)為輸入流問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java遠(yuǎn)程文件url怎么轉(zhuǎn)為輸入流”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
URL url = new URL(fileUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //設(shè)置超時間為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 { //創(chuàng)建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標(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(); }
//文件訪問路徑 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; }
到此,關(guān)于“java遠(yuǎn)程文件url怎么轉(zhuǎn)為輸入流”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。