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();
    }

 

posted @ 2018-12-04 19:50  何其小静  阅读(4478)  评论(0编辑  收藏  举报