如果你也遇到和我一样使用newFile本地文件可以通过,可是一旦路径变成http的绝对路径就会报找不到文件的错误,可以用下面的方式来解决
啥都不多说,具体解决方式如下(亲测有效)
public getAllFieldByExcel(String filePath) {
URL url = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
url = new URL(filePath);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(150000000);
connection.setReadTimeout(600000000);
connection.connect();
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
}
Workbook wb = null;
if (fileType.toLowerCase().equals("xls")) {
wb = new HSSFWorkbook(inputStream);
} else {
wb = new XSSFWorkbook(inputStream);
}
Sheet sheet = wb.getSheetAt(0);
if (endDataRow == null) {
endDataRow = sheet.getLastRowNum();
}
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
return ReturnUtils.NOErrorMsg("找不到文件");
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
return ReturnUtils.NOErrorMsg(e.toString());
}finally{
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();
}
return “”;
}
最后我也没搞懂,为什么new File的时候本地文件没有问题,可是远程文件就会报错。先找到解决办法,以后再想吧,如果有知道原因的网友,欢迎评论区为我解惑,谢谢