spring + ajaxfileupload 返回文件名中文亂碼

問題在於:

	@ResponseBody  // 這裏去掉
    @RequestMapping(params = "method=uploadFile")
    public void uploadFile(@RequestParam("file") List<MultipartFile> file, 
    	HttpServletRequest request, 
    	HttpServletResponse response // 添加
    ) {

        response.setContentType("text/html;chartset=UTF-8");  // 添加
        response.setCharacterEncoding("UTF-8"); // 添加
        //把文件放在部署路徑下的用戶ID文件夾中
        String webPath = request.getSession().getServletContext().getRealPath("/");

        String filePath = "uploaddir/";

        File dir = new File(webPath+filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        List<Map<String,Object>> resultList = new ArrayList<>();
        Map<String, Object> result = null;
        Map<String, Object> resultmap = new HashMap<>();
        try {
            for(MultipartFile myfile : file){

                String filename = getFilename(myfile.getOriginalFilename());
                String newFileName = System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
                String uuid= AppUtil.getUUID();
                //上傳文件
                File tfile = new File(webPath+filePath, newFileName);
                myfile.transferTo(tfile);
                //將文件名和文件路徑返回
                result = new HashMap<String, Object>();
                result.put("ID", uuid);// 文件添加一個ID
                result.put("filename", filename);//原文件名
                result.put("newFileName", newFileName);//新文件名
                result.put("filepath", filePath + newFileName);//文件全路徑
                result.put("fullfilepath", webPath+filePath + newFileName);//文件全路徑

                resultList.add(result);
            }
            resultmap.put("success", "1");
            resultmap.put("list", resultList);
            response.getWriter().write(JSON.toJSONString(resultList)); // 添加
        } catch (IOException e) {
            resultmap.put("success", "0");
            logger.error(e.getMessage());
        }
        String str = JSON.toJSONString(resultmap, SerializerFeature.DisableCircularReferenceDetect);
        logger.debug("結果:{}", str);// debug 輸出結果
//        return str;   //  不能有返回值
    }

ajaxfileupload 不需要改什麼東西

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章