直接看代码,如果前台页面不带有上传文件,就进不了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);
}