newFile()一个http绝对路径的文件报文件找不到错误的解决方式

如果你也遇到和我一样使用newFile本地文件可以通过,可是一旦路径变成http的绝对路径就会报找不到文件的错误,可以用下面的方式来解决

啥都不多说,具体解决方式如下(亲测有效)

 public  getAllFieldByExcel(String filePath) {
  
        URL url = null;
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        try {
            url = new URL(filePath);
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接方式:get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(150000000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(600000000);

            // 发送请求
            connection.connect();
            // 通过connection连接,获取输入流
            if (connection.getResponseCode() == 200) {
                inputStream = connection.getInputStream();
            }
            // 根据指定的文件输入流导入Excel从而产生Workbook对象
            Workbook wb = null;
            if (fileType.toLowerCase().equals("xls")) {
                wb = new HSSFWorkbook(inputStream);
            } else {
                wb = new XSSFWorkbook(inputStream);
            }
            // 获取Excel文档中的第一个表单
            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的时候本地文件没有问题,可是远程文件就会报错。先找到解决办法,以后再想吧,如果有知道原因的网友,欢迎评论区为我解惑,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要将Java文件转换为Base64编码,首先确保要转换的文件存在,并且在您的代码中正确指定了文件路径。如果不到文件,可能有以下几个原因: 1. 文件路径错误:请确认您提供的文件路径是正确的,并且文件确实存在于该路径下。可以尝试使用绝对路径来确保准确性。 2. 文件权限问题:请确保您的代码具有足够的权限来访问该文件。在某些情况下,您可能需要以管理员身份运行程序。 3. 文件不存在:如果文件确实不存在,您可以检查文件名的拼写错误,并确保文件已经创建或上传到正确的位置。 以下是一个Java文件转换为Base64的示例代码,您可以参考: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Base64; public class FileToBase64Converter { public static void main(String[] args) { String filePath = "path/to/your/file.java"; File file = new File(filePath); if (file.exists()) { try { FileInputStream fileInputStream = new FileInputStream(file); byte[] fileBytes = new byte[(int) file.length()]; fileInputStream.read(fileBytes); fileInputStream.close(); String base64String = Base64.getEncoder().encodeToString(fileBytes); System.out.println(base64String); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("File not found!"); } } } ``` 请将`filePath`替换为要转换的Java文件的实际路径。如果文件存在且路径正确,此代码将打印文件的Base64编码字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值