HttpServletRequest获取参数和文件

从HttpServletRequest中获取上传文件及参数的方法

try {
    //下面这句必须加,不然报错
    MultipartResolver resolver = new CommonsMultipartResolver(hsq.getSession().getServletContext());
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) hsq;
​
    //获取一起跟文件传过来的其他参数值
    String id = multipartRequest.getParameter("id");
​
    //获取上传上来的文件
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
        MultipartFile file = entry.getValue();
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        String path = "C:\\Users\\user\\Desktop\\" + fileName;
        file.transferTo(new File(path));
    }
} catch (Exception e) {
    e.printStackTrace();
}

 

转载自:关于使用Springmvc的MultipartHttpServletRequest来获得表单上传文件的问题,万分感谢~_百度知道 (baidu.com)

 

 
posted @ 2021-12-06 10:45  散人长情  阅读(5062)  评论(0编辑  收藏  举报