如果你也遇到和我一樣使用newFile本地文件可以通過,可是一旦路徑變成http的絕對路徑就會報(bào)找不到文件的錯(cuò)誤,可以用下面的方式來解決
啥都不多說,具體解決方式如下(親測有效)
public getAllFieldByExcel(String filePath) {
URL url = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
url = new URL(filePath);
// 通過遠(yuǎn)程url連接對象打開一個(gè)連接,強(qiáng)轉(zhuǎn)成httpURLConnection類
connection = (HttpURLConnection) url.openConnection();
// 設(shè)置連接方式:get
connection.setRequestMethod("GET");
// 設(shè)置連接主機(jī)服務(wù)器的超時(shí)時(shí)間:15000毫秒
connection.setConnectTimeout(150000000);
// 設(shè)置讀取遠(yuǎn)程返回的數(shù)據(jù)時(shí)間:60000毫秒
connection.setReadTimeout(600000000);
// 發(fā)送請求
connection.connect();
// 通過connection連接,獲取輸入流
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
}
// 根據(jù)指定的文件輸入流導(dǎo)入Excel從而產(chǎn)生Workbook對象
Workbook wb = null;
if (fileType.toLowerCase().equals("xls")) {
wb = new HSSFWorkbook(inputStream);
} else {
wb = new XSSFWorkbook(inputStream);
}
// 獲取Excel文檔中的第一個(gè)表單
Sheet sheet = wb.getSheetAt(0);
if (endDataRow == null) {
endDataRow = sheet.getLastRowNum();
}
//此處已經(jīng)可以寫你的具體邏輯了
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
return ReturnUtils.NOErrorMsg("找不到文件");
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
return ReturnUtils.NOErrorMsg(e.toString());
}finally{
//遠(yuǎn)程連接記得關(guān)閉
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 關(guān)閉遠(yuǎn)程連接
}
return “”;
}
最后我也沒搞懂,為什么new File的時(shí)候本地文件沒有問題,可是遠(yuǎn)程文件就會報(bào)錯(cuò)。先找到解決辦法,以后再想吧,如果有知道原因的網(wǎng)友,歡迎評論區(qū)為我解惑,謝謝
文章來源地址http://www.zghlxwxcb.cn/news/detail-601774.html
文章來源:http://www.zghlxwxcb.cn/news/detail-601774.html
到了這里,關(guān)于newFile()一個(gè)http絕對路徑的文件報(bào)文件找不到錯(cuò)誤的解決方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!