MultipartFile 获取上传TXT文件字数


@ResponseBody
    @RequestMapping(value = "/addImgForDynamic")//(发布动态)
    public Map addImgForDynamic(MultipartFile uploadFile) {
        Map<String,Object> result = new HashMap();
        try {
            String oo =convertStreamToString(uploadFile);
            System.out.println("" + oo);
            System.out.println("" + oo.length());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public String convertStreamToString(MultipartFile file){
        StringBuilder sb;
        try (InputStream inputStream = file.getInputStream()) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "/n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM