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 不需要改什么东西

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值