2 ti baby hu ti_baby_hu 于 2016.09.20 15:37 提问

springmvc 上传MultipartFile 5C

直接看代码,如果前台页面不带有上传文件,就进不了controller,

     @RequestMapping(value="/addApplicationFile")   
         public  void  addApplicationFile( ApplicationFile  applicationFile,HttpServletRequest request,HttpServletResponse response,@RequestParam(value="file", required=false) MultipartFile file){ 
                Map<String, Object>   resultMap = new HashMap<String,Object>();
                boolean   localUploadStatus = false;
                boolean   ftpUploadStatus = false;
                OptionsItems    optionsItems = null;
             Long userId = null;
                FtpFileList ftpFileList  = null;
                try {
                      userId = SystemUtil.getUserId(request);   //设置请求编码  
                      if( null == userId ){
                            SystemUtil.userInvalid(request, response);
                            return;
                      }

                      if(    
                                null == applicationFile ||
                                null == applicationFile.getFileName() ||  applicationFile.getFileName().equals("") ||
                                null == applicationFile.getApplicationVersion() || applicationFile.getApplicationVersion().equals("") ||
                                null == applicationFile.getApplicationName() ||  applicationFile.getApplicationName().equals("") ||
                                null == applicationFile.getFileType() || applicationFile.getFileType().equals("") 
                        ){
                           resultMap.put(Constant.SHRG_STATUS, Constant.RESULT_STATUS_ERROR);
                             resultMap.put(Constant.SHRG_MESSAGE, Constant.REQUEST_PARAM_ERROR);
                             SystemUtil.outputJsonObject(resultMap, response);
                             return;
                        }
                         int length = applicationFileService.validateApplicationName(applicationFile);
                         if(length>0){
                             resultMap.put(Constant.SHRG_FLAG, Constant.RESULT_STATUS_FLAG);
                             resultMap.put(Constant.SHRG_MESSAGE, Constant.APPLICATION_FILE_IS_EXIST);
                             SystemUtil.outputJsonObject(resultMap, response);
                             return;
                         }
                        //先上传再入库
                        if (null != file && !file.isEmpty()) {
                            //文件上传路径
                            StringBuffer realPath = new StringBuffer();//tomcat相对路径
                            realPath.append(Constant.PATH_SEPARATOR_SLASH).append(Constant.PATH_APPLICATION_UPLOAD);//PATH_APPLICATION_UPLOADnew String(Constant.PATH_APPLICATION_UPLOAD.getBytes("GBK"),"iso-8859-1")
                            //在数据得到上传的类型
                            optionsItems =  new OptionsItems();
                            optionsItems.setModule(Constant.INFO_DIFF);
                            optionsItems.setOptionCode(Constant.APP_UPLOAD_FILE_TYPE);
                            List<OptionsItems> listO = optionsItemsService.initOptionsItem(optionsItems);
                            optionsItems =  new OptionsItems();
                            optionsItems.setModule(Constant.TER);
                            optionsItems.setOptionCode(Constant.SOFTWARE_UPDATE_FILE_TYPE);
                            List<OptionsItems> listT = optionsItemsService.initOptionsItem(optionsItems);
                            //执行上传
                            if(applicationFile.getUploadType() != null && Constant.UPLOAD_TYPE_A.equals(applicationFile.getUploadType())){
                                String fileType = listO.get(0).getAttribute1();
                                fileType = fileType.replaceAll("\\.",   "");
                                ftpFileList = FileUpload.upload(file,realPath.toString(), fileType,request);
                            }else{
                                String fileType = listT.get(0).getAttribute1();
                                fileType = fileType.replaceAll("\\.",   "");
                                ftpFileList = FileUpload.upload(file,realPath.toString(),fileType,request);
                            }
                            if(null !=ftpFileList)
                                localUploadStatus = true;
                            ftpUploadStatus = FtpManager.getInstance().uploadFile(ftpFileList.getPath(), ftpFileList.getFtpFilename(), ftpFileList.getFtpPath());
                            FileUtil.delFile(ftpFileList.getPath(),ftpFileList.getFtpFilename());//删除本地文件 

                            applicationFile.setFileSize(ftpFileList.getFileSize()+"");
                            applicationFile.setFileType(ftpFileList.getFileSuffix());
                            applicationFile.setFtpFileName(ftpFileList.getFtpFilename());   
                            applicationFile.setFtpFilePath(ftpFileList.getFtpPath()+Constant.PATH_SEPARATOR_SLASH);
                            applicationFile.setCreatedBy(userId);
                            applicationFile.setLastUpdateBy(userId);
                            //文件上传之后,再将文件 入库
                            applicationFileService.addApplicationFile(applicationFile);
                            resultMap.put(Constant.SHRG_STATUS, Constant.RESULT_STATUS_SUCCESS);
                             resultMap.put(Constant.SHRG_MESSAGE, Constant.ADD_SUCCESS);
                        }else{
                            resultMap.put(Constant.SHRG_STATUS, Constant.RESULT_STATUS_ERROR);
                             resultMap.put(Constant.SHRG_MESSAGE, Constant.UPLOAD_FILE_IS_NULL);
                        }
                        SystemUtil.outputJsonObject(resultMap, response);
             }catch(Exception ex){
                 ex.printStackTrace();
                    if(ftpUploadStatus){
                        String deletePath = ftpFileList.getFtpPath()+Constant.PATH_SEPARATOR_SLASH +ftpFileList.getFtpFilename();
                        try {
                            FtpManager.getInstance().removeFile(deletePath);
                        } catch (Exception e) {
                            int resultCodeA = ShrgLog.exceptionLogMap(this, Constant.ERROR_LEVEL, Constant.INFO_DIFFUSION_PUBLISH, Constant.ADD_UPLOAD_APPLICATION_FILT, e,userId);
                            SystemUtil.exceptionHandle(response, resultCodeA);
                        }
                    }else if(localUploadStatus){
                        FileUtil.delFile(ftpFileList.getPath(),ftpFileList.getFtpFilename());//删除本地文件 
                    }
                    int resultCode = ShrgLog.exceptionLogMap(this, Constant.ERROR_LEVEL, Constant.INFO_DIFFUSION_PUBLISH, Constant.ADD_UPLOAD_APPLICATION_FILT, ex,userId);
                    SystemUtil.exceptionHandle(response, resultCode);

             }

3个回答

Royal_lr
Royal_lr   Ds   Rxr 2016.09.20 16:40

进不了请求,,前台会报错吗

ti_baby_hu
ti_baby_hu 回复ti_baby_hu: 后台也报了说是不是MultipartFile 这位的类型
大约一年之前 回复
ti_baby_hu
ti_baby_hu 前台404
大约一年之前 回复
ios_king
ios_king   2016.09.20 17:05

在springmvc 中 如果是使用RequestParam 指定参数,当这个参数没有的时候他就会报请求错误 异常

ti_baby_hu
ti_baby_hu 不指定的话也是会报错误的啊。说是找不到路径,我用的是百度上传插件
大约一年之前 回复
ios_king
ios_king   2016.09.21 16:44

那就是你前台有问题,一般要用表单提交。还要记得加上 表单上传 必须的属性

Csdn user default icon
上传中...
上传图片
插入图片