MultipartFile 获取上传TXT文件字数

主要思路  MultipartFile 获得输入流  流 转 String  length()

@ResponseBody
@RequestMapping(value = "/addImgForDynamic")//(发布动态)
public Map addImgForDynamic(MultipartFile uploadFile){
    Map result=new HashMap();
    try {
       String oo = GetFileWordCount.convertStreamToString(uploadFile.getInputStream());
        System.out.println(""+oo);
        System.out.println(""+oo.length());
    } catch (IOException e) {
        e.printStackTrace();
    }


public static String convertStreamToString(InputStream inputStream){
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder 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();
}

你可能感兴趣的:(JAVA)