关于文件上传

前台代码:

<form id="ff" method="post" enctype ="multipart/form-data">
                <table style="margin: auto;">
                    <tr>
                        <td>短信内容:</td>
                        <td>
                            <textarea id="sms_content" name="sms_content" rows="3" cols="20"  ></textarea>
                        </td>
                    </tr>   
                    <tr>
                        <td>发送目标手机号码:</td>
                        <td>
                            <input type="file" placeholder="(一行一个号码)" name="mobileNoFile" id="mobileNoFile"/>
                        </td>
                    </tr>   
                </table>
            </form>

后台代码:

    @RequestMapping(value = "/import")
    @ResponseBody
    public Map<String, Object> importMobileNo(@RequestParam Map<String, String> data, HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        // request 处理
        MultipartHttpServletRequest mRequest = null;
        CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());
        if(cmr.isMultipart(request)){
            if(request instanceof MultipartHttpServletRequest) {
                mRequest= (MultipartHttpServletRequest)(request);
            }else{
                cmr.setDefaultEncoding("utf-8");
                mRequest = cmr.resolveMultipart(request);
            }
        }
        // 获取上传的文件
        File uploadFile = this.getUploadFile(mRequest);
        String sms_content = mRequest.getParameter("sms_content");
    ....
        return result;
    }

private File getUploadFile(MultipartHttpServletRequest mRequest) throws Exception{
        String realPath = mRequest.getSession().getServletContext().getRealPath(Constants.UPLOAD_FILE_PATH);
        File localFile = null;
        Iterator<String> files = mRequest.getFileNames();
        while(files.hasNext()){
            MultipartFile mFile = mRequest.getFile(files.next());
            if(mFile != null){
                String fileName = UUID.randomUUID()+mFile.getOriginalFilename();
                String path = realPath + File.separator +fileName;

                localFile = new File(path);
                mFile.transferTo(localFile);
            }
        }
        return localFile;
    }

需要注意的几个地方:

  1. form 中要指定 enctype ="multipart/form-data", 否则将无法获取到上传的文件

    cmr.isMultipart(request)
    

    会返回 false
    enctype 的值有3种:

  2. 指定了 enctype ="multipart/form-data" 以后, 在后台从 request 中就获取不到sms_content文本域的数据了. 而需要从MultipartHttpServletRequest 中获取

  3. 类型转换失败的异常

java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

原因: 是强制将 HttpServletRequest转换为 MultipartHttpServletRequest, 在转换要做判断,如果这个 request 并不是 MultipartHttpServletRequest ,需要借助 CommonsMultipartResolver 来实现类型转换

 CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());
 if(cmr.isMultipart(request)){
   // mRequest= (MultipartHttpServletRequest)(request);
    if(request instanceof MultipartHttpServletRequest) {
        mRequest= (MultipartHttpServletRequest)(request);
    }else{
        cmr.setDefaultEncoding("utf-8");
        mRequest = cmr.resolveMultipart(request);
    }
}

参考:

2015-08-27 15:402