Java中語音url轉(zhuǎn)換成InputStream的示例代碼
Java中語音url轉(zhuǎn)換成InputStream
在Java中,你可以使用java.net.URL和java.net.URLConnection類來將語音URL轉(zhuǎn)換為InputStream。以下是一個簡單的示例:
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class AudioUrlToInputStream {
public static void main(String[] args) {
String audioUrl = "https://example.com/audio.mp3"; // 替換為實(shí)際的語音URL
try {
InputStream inputStream = getInputStreamFromUrl(audioUrl);
// 在這里使用inputStream進(jìn)行后續(xù)操作,例如寫入文件或進(jìn)行語音處理
// ...
// 記得關(guān)閉InputStream
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static InputStream getInputStreamFromUrl(String urlString) throws IOException {
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
return urlConnection.getInputStream();
}
}在這個示例中,getInputStreamFromUrl方法通過URL和URLConnection獲取語音URL對應(yīng)的InputStream。你可以將實(shí)際的語音URL替換為你想要處理的URL。在main方法中,你可以使用獲取到的InputStream執(zhí)行后續(xù)操作,比如將語音寫入文件或進(jìn)行其他處理。最后,記得關(guān)閉InputStream以釋放資源。
補(bǔ)充:
java 遠(yuǎn)程文件url 轉(zhuǎn)為輸入流
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 校驗(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();
}到此這篇關(guān)于Java中語音url轉(zhuǎn)換成InputStream的示例代碼的文章就介紹到這了,更多相關(guān)java url轉(zhuǎn)換InputStream內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Spring從YAML文件讀取內(nèi)容映射為Map方式
這篇文章主要介紹了使用Spring從YAML文件讀取內(nèi)容映射為Map方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringCloud中的Hystrix保護(hù)機(jī)制詳解
這篇文章主要介紹了SpringCloud中的Hystrix保護(hù)機(jī)制詳解,Hystrix,英文意思是豪豬,全身是刺,看起來就不好惹,是一種保護(hù)機(jī)制,Hystrix也是Netflix公司的一款組件,需要的朋友可以參考下2023-12-12
Hibernate中實(shí)現(xiàn)增刪改查的步驟詳解
本篇文章主要介紹了Hibernate中實(shí)現(xiàn)增刪改查的步驟與方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
SpringBoot多數(shù)據(jù)源的兩種實(shí)現(xiàn)方式實(shí)例
最近在項(xiàng)目開發(fā)中,需要為一個使用MySQL數(shù)據(jù)庫的SpringBoot項(xiàng)目,新添加一個PLSQL數(shù)據(jù)庫數(shù)據(jù)源,下面這篇文章主要給大家介紹了關(guān)于SpringBoot多數(shù)據(jù)源的兩種實(shí)現(xiàn)方式,需要的朋友可以參考下2022-04-04
解決IDEA報(bào)錯Failed?to?start?bean‘documentationPluginsBootstra
這篇文章主要介紹了解決IDEA報(bào)錯Failed?to?start?bean‘documentationPluginsBootstrapper‘問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Java入門基礎(chǔ)之常規(guī)的命名方法和變量的值及其引用
這篇文章主要介紹了Java的命名方法和變量的值及其引用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
MyBatis不同Mapper文件引用resultMap實(shí)例代碼
這篇文章主要介紹了mybatis 不同Mapper文件引用resultMap的實(shí)例代碼,非常不錯具有參考借鑒價值,需要的朋友可以參考下2017-07-07
Java中正則表達(dá)式的語法以及matches方法的使用方法
正則表達(dá)式(Regular Expression)是一門簡單語言的語法規(guī)范,是強(qiáng)大、便捷、高效的文本處理工具,這篇文章主要給大家介紹了關(guān)于Java中正則表達(dá)式的語法以及matches方法的使用方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05

