springboot文件读取方法(multipartFile和file两种)

只是记录一下,供自己学习使用。

 public String readCityFile(String url) {
        File file = new File(url);
        StringBuilder stringBuilder = null;
        FileInputStream is;
        try {
            if (file.length() != 0) {
                is = new FileInputStream(file);
                InputStreamReader streamReader = new InputStreamReader(is);
                BufferedReader reader = new BufferedReader(streamReader);
                String line;
                stringBuilder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                reader.close();
                is.close();
            } else {
                stringBuilder.append("空的");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        String str = String.valueOf(stringBuilder);
        if (str == "null" || str.isEmpty() || str == "") {
            str = "该文件无法在线预览或存在异常";
        }
        return str;
    }

 传进来的参数为MultipartFile类型,直接读取代码如下:

private String readCityFile(MultipartFile multipartFile) {
    StringBuilder stringBuilder = null;
    try {
        if (multipartFile!=null) {
            InputStream bb = multipartFile.getInputStream();
            InputStreamReader streamReader = new InputStreamReader(bb);
            BufferedReader reader = new BufferedReader(streamReader);
            String line;
            stringBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            reader.close();
            bb.close();
        } else {
            stringBuilder.append("空的");           
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return String.valueOf(stringBuilder);
}

转载:https://blog.csdn.net/suihedaren/article/details/118355244

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot项目使用MultipartFile来上传文件,可以按照以下步骤: 1. 在前端页面添加文件上传表单,例如: ```html <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <button type="submit">上传</button> </form> ``` 2. 在后端Controller添加文件上传处理方法,例如: ```java @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "上传失败,请选择文件"; } try { // 获取文件名 String fileName = file.getOriginalFilename(); // 获取文件的字节数组 byte[] bytes = file.getBytes(); // TODO: 保存文件到指定位置 return "上传成功"; } catch (IOException e) { e.printStackTrace(); return "上传失败:" + e.getMessage(); } } ``` 在上面的代码,我们首先检查上传的文件是否为空,然后获取文件名和文件的字节数组,并将其保存到指定位置。请注意,由于我们使用了`enctype="multipart/form-data"`,因此必须使用`@RequestParam`注解来获取文件参数。 3. 在SpringBoot的`application.properties`文件配置文件上传的最大限制大小,例如: ```properties spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB ``` 在上面的配置,我们将文件上传的最大限制大小设置为10MB。如果上传的文件大小超过了这个限制,将会抛出`MaxUploadSizeExceededException`异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值