jsp代码
<td><label>文件名称:</label></td>
<td align="left"><input class="easyui-filebox" required="true" multiple="true" id="uploadFileWatermark1"
data-options="validType:['fileNamecheck']" name="file" buttonText="选择文件" style="width:100%;height:26px"></td>
<td>
<a href="javascript:void(0)" class="easyui-linkbutton c6 btn-ok"
iconCls="icon-ok" onclick="importDateWatermark();" style="width: 90px"><msg:message key="COMMENT_SAVE"/></a>
</td>
js:
// 上传文件并添加水印*
var uploadFileOK = false;
function importDateWatermark() {
var uploadFileName= $('#uploadFileWatermark1').filebox('getValue');
var arr = new Array();
arr = uploadFileName.split("\\");
var name = arr[arr.length-1];
var suffix = name.split(".")[name.split(".").length-1];
if(suffix=="xlsx" || suffix=="xls"){
name= name.split(".")[0]+".pdf";
}
onloading();//开启加载效果
$('#fmWatermark1').form('submit', {
url: rootPath + 'a/ordermanage/GSPO001/watermarkUpload',
onSubmit: function () {
return $(this).form('validate');
},
success: function (data) {
var result = JSON.parse(data);
var condition = this;
if (result.success) {
$('#uploadFileName2').textbox('setValue',name);
$.messager.alert('提示信息', result.messageContent, 'info');
// datagridReload(condition);
$('#dlgWatermark1').dialog('close');
uploadFileOK = true;
} else {
$.messager.alert('提示信息', result.messageContent, 'error');
removeload();// 关闭加载效果
}
removeload();// 关闭加载效果
}
});
}
controller:
/**
* 上传文件并添加水印
* @param file
* @param redirectAttributes
* @return
*/
@RequestMapping("watermarkUpload")
@ResponseBody
public String watermarkUpload(@RequestParam("file")CommonsMultipartFile[] file,RedirectAttributes redirectAttributes){
MessageInfo msgInfo=new MessageInfo();
String remarks="";
try {
msgInfo = gSPO001Service.watermarkUpload(file);
} catch (BusinessException ex) {
msgInfo =ex.getMsgInfo(); //获取消息异常
logger.error("机能:{},追加操作出现异常:",JobIds.GSPT008);
logger.error(msgInfo.getMessageContent());
this.logUtilService.writeLog("GSPT008-文件上传履历管理-追加异常", msgInfo);
}catch (Exception e) {
logger.error("机能:{},追加操作出现非业务异常:",JobIds.GSPT008);
logger.error(e.getMessage());
msgInfo = PropertiesUtil.loadSuccessMsg(MsgIds.CME03019);
this.logUtilService.writeLog("GSPT008-文件上传履历管理-追加异常", msgInfo);
}finally{
file = null;
}
return JSONObject.fromObject(msgInfo).toString();
}
service:
/**
* 上传文件并添加水印
* @param files
* @return
* @throws BusinessException
*/
// @Transactional(readOnly=false)
public MessageInfo watermarkUpload(CommonsMultipartFile[] files)throws BusinessException{
MessageInfo msgInfo = new MessageInfo();
String finalPath = "";
String backupsPath = "";
try {
finalPath = new PropertiesLoader("destinationPath.properties").getProperty("GSPO001Path");
File file1 = new File(finalPath);
if (file1.exists()) {
file1.mkdirs();
}
backupsPath = new PropertiesLoader("destinationPath.properties").getProperty("GSPO001PathBackups");
File file2 = new File(finalPath);
if (file2.exists()) {
file2.mkdirs();
}
String[] fileNameArr = null;
String filePreName = "";//文件名前缀,文件名
String fileSufName = "";//文件名后缀,格式名
//循环做数据提取
for(int i = 0;i<files.length;i++){
String fileDir="";
String file ="";
fileNameArr = files[i].getOriginalFilename().split("\\.");
if(fileNameArr==null||fileNameArr.length!=2){
logger.error("机能:{},上传文件名有异常:文件名为{}:",JobIds.GSPT008,files[i].getOriginalFilename());
throw new BusinessException(MsgIds.GSPPDBSQLE002,"文件异常");
}
filePreName = fileNameArr[0].toUpperCase();
fileSufName =fileNameArr[1];
file = filePreName+"."+fileSufName;
fileDir = backupsPath+File.separator+file;
files[i].transferTo(new File(fileDir));//上传到指定路径
// //生成水印图片
String drawWaterPath = ImageUtils.graphicsGeneration("draw");
String eciWaterPath = ImageUtils.graphicsGeneration("eci");
String outputFile = finalPath+File.separator+filePreName;
String suffix = fileSufName;
if(suffix.toUpperCase().equals("TIF")) {
ImageWatermarkUtil2.ImageByTexts2(fileDir,outputFile+".tif",drawWaterPath);
}else if(suffix.toUpperCase().equals("PDF")){
AddWatermark.waterMark1(fileDir,outputFile+".pdf",eciWaterPath);
}else if(suffix.toUpperCase().equals("XLSX")){
// 先把Excel转为PDF
AddWatermark.excelToPdf(fileDir,backupsPath+filePreName+".pdf");
//再给pdf加水印
AddWatermark.waterMark1(backupsPath+filePreName+".pdf",outputFile+".pdf",eciWaterPath);
}
//删除原文件
deleteFiles(backupsPath);
}
msgInfo =PropertiesUtil.loadSuccessMsg(MsgIds.COMMENTADD002, JobIds.GSPT008);
} catch (BusinessException be) {
logger.error("机能:{},业务异常,追加操作出现异常:",JobIds.GSPT008);
logger.error(be.getMsgInfo().getMessageContent());
throw be;
} catch (IOException e) {
logger.error("机能:{},非业务异常,追加操作出现异常:",JobIds.GSPT008);
logger.error(e.getMessage());
//捕捉未定义的异常,SQL错误到处异常信息
throw new BusinessException(MsgIds.CME03019);// 数据库异常
} catch (Exception e) {
if(e.getCause().toString().contains("ORA-00001")){
//主键重复
msgInfo =PropertiesUtil.loadMsg(MsgIds.GSPT008UP, JobIds.GSPT008);
return msgInfo;
}
logger.error("机能:{},非业务异常,追加操作出现异常:",JobIds.GSPT008);
logger.error(e.getMessage());
throw new BusinessException(MsgIds.CME03019);// 数据库异常
}
return msgInfo;
}