上传TXT文件后,再打开没有问题。但上传word文件后,打开里面内容就变成了乱码
@ResponseBody @RequestMapping(value = "upload", method = RequestMethod.POST) public Object uploadCon( HttpServletRequest request, HttpServletResponse response, MultipartFile file, Contract contract) throws IOException { Map<String, Object> map = new HashMap<String, Object>(); File fileDir = new File(UPLOAD_FILE_PATH); // 当目录不存在时创建 if (!fileDir.exists()) { fileDir.mkdir(); } FileWriter fileWriter = new FileWriter(UPLOAD_FILE_PATH + file.getOriginalFilename()); map.put("c_content", UPLOAD_FILE_PATH + file.getOriginalFilename()); map.put("c_filenm", file.getOriginalFilename()); System.out.println(UPLOAD_FILE_PATH + file.getOriginalFilename()); InputStream inputStream = file.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader( inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); JSONObject resObj = new JSONObject(); resObj.put("code", 0); resObj.put("data", map); resObj.put("msg", "ok"); int hasRead = 0; char[] buff2 = new char[1024]; while ((hasRead = bufferedReader.read(buff2)) > 0) { fileWriter.write(buff2); } fileWriter.close(); bufferedReader.close(); System.out.println("上传成功"); return resObj; }
忽然笑
ABOUTYOU
相关分类