spring中,文件上传对文件大小的处理(一)

使用spring上传文件,有对文件上传大小的限制,但是使用官方的配置并无法捕捉到异常的信息。

方案一:首先介绍一下spring对文件大小的设置(这种方式,我还没有实现,有点纠结;超出文件大小,总是报MaxUploadSizeExceededException异常,并没有在异常出现后定位到错误页面error.jsp页面)

需要说明一点:对于这种方式,文件上传方法并不会执行(也就是:不会执行controller方法),而是先报MaxUploadSizeExceededException异常。

spring中xml配置:

<!-- 首先配置上传文件的大小 -->
<
bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- <property name="resolveLazily" value="true"/> --> <property name="maxUploadSize" value="20971520" /> <property name="defaultEncoding" value="UTF-8"></property> </bean> <!-- 首先配置上传文件的大小超出20M后,异常处理 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!-- 遇到MaxUploadSizeExceededException异常时,跳转到error.jsp页面 --> <prop key= "org.springframework.web.multipart.MaxUploadSizeExceededException">/exception/error</prop> </props>
   </property> </bean>

jsp中:

<body>
    <div class="file_upload" id="_file_upload">
        <div id="dialog-form" title="文件上传">
            <form id="dialog-form-fileForm" action="${pageContext.request.contextPath}/${key }/${modelCode }/uploadFile" method="post"
                enctype="multipart/form-data" target="uploadFrame">
                <table style="margin: 20px auto 0;width: 70%;">
                    <tr align="center" height="40">
                        <td colspan="5" style="text-align: center;" >
                            <button type="button" id="up_formSubmit"
                                onclick="_File_Utils.submitFile('#dialog-form')">
                                <span class="glyphicon glyphicon-arrow-up"></span> 上传
                            </button>
                        </td>
                    </tr>
                    <tr align="center" height="32">
                        <td style="min-width: 120px;" align="right"><input
                            type="checkbox" title="请选择勾选复选框!">&nbsp;&nbsp;保存名称&nbsp;</td>
                        <td style="min-width: 280px;"><input class="fileupload-name"
                            type="text" value="" size="15" name="fileName"
                            title="请填写上传文件保存名称!"></td>
                        <td align="left"><input type="file"
                            style="width: 340px; margin-left: 20px; height: 22px;" id="file"
                            name="file" onchange="_File_Utils.ckFile(this)"></td>
                    </tr>
                    <tr id="dialog-form-bt" height="40">
                        <td colspan="5" align="center">&nbsp;</td>
                    </tr>
                </table>
            </form>
        </div>
        <iframe name="uploadFrame" src="" height="0px" style="border: 0px;"></iframe>
    </div>
    <script src="${pageContext.request.contextPath}/uploadFile.js?t=<%=System.currentTimeMillis() %>"></script>
</body>

controller方法实现文件上传:(出现文件大小异常,该方法是不执行的,其中SysFileUpload是bean类,可以根据自己实际情况进行实现)

@RequestMapping(value = "/{key}/{modelCode}/uploadFile", method = RequestMethod.POST)
    public String uplopadFile(MultipartHttpServletRequest Request, Model model, @PathVariable("key") String key,
       @PathVariable("modelCode") String modelCode,@RequestParam("file") MultipartFile[] files, @RequestParam("fileName") String[] fileNames) {
        List<SysFileUpload> listFile = new ArrayList<SysFileUpload>();
        List<SysFileUpload> retlistFile = new ArrayList<SysFileUpload>();
        JSONObject retMsg = new JSONObject();
        int code = 0;
        try {
            for (int i = 0; i < files.length; i++) {
                MultipartFile f = files[i];
                // 文件名称处理
                if(f.getSize()<=20971520){
                    String fileName = fileNames[i];
                    String passName = f.getOriginalFilename();
                    String fileSuffix = passName.substring(passName.lastIndexOf("."));
                    if (fileName.indexOf(fileSuffix) == -1) {
                        fileName = fileName + fileSuffix;
                    }
                    String juid = JMToolsCoreUtil.javaUUID();
                    listFile.add(new SysFileUpload(fileName, f.getInputStream(), juid));
                    retlistFile.add(new SysFileUpload(fileName, juid));
                }else{
                    code=-99;
                }
            }
            if(code==0){
                int i = dp.addFile(listFile);
                retMsg.element("msg", retlistFile);
                retMsg.element("sum", i);
            }
        } catch (Exception e) {
            code = -1;
        }
        retMsg.element("code", code);
        model.addAttribute("opMsg", retMsg.toString());
        model.addAttribute("model", modelCode);
        return "txgl/uploadFrame";
    }

 

posted @ 2017-07-13 20:51  小码农成长记  阅读(803)  评论(0)    收藏  举报