编辑
function editChannel(){
var row = $('#dg').datagrid('getSelected');
if (row){
$('#dlg2').dialog('open').dialog('center').dialog('setTitle','编辑');
$('#fm2').form('load',row);
url = '/channel/update.html';
}else{
$.messager.show({
title: '提示信息',
msg: '请选择一条记录!'
});
}
}
/**
* 编辑保存
*/
function save2Channel(){
$('#fm2').form('submit' , {
url : '/channel/update.html',
onSubmit: function(){
},
success : function(data){
$.messager.progress('close');
var data = eval('('+data+')');
if (data && data.success){
$.messager.show({
title: '成功信息',
msg: data.msg
});
$('#dlg2').dialog('close');
$('#dg').datagrid('reload');
} else {
$.messager.show({
title: '失败信息',
msg: data.msg
});
$('#dlg2').dialog('close');
$('#dg').datagrid('reload');
}
}
});
var win = $.messager.progress({
title:'请稍后',
msg:'正在上传数据...'
});
}
///*ChannelRequest request*/ 注不能直接传请求对象
@SuppressWarnings("unused")
@RequestMapping("/update")
@ResponseBody
public Result update(HttpServletRequest request0,Integer channelId ,Character status, Character showFlag
,Character flag,String channelName,String fileParserName,String fileCompareName ,String importorName
,String rule ,String sourceConfig,String schemaInfo, String processors){
try{
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request0, MultipartHttpServletRequest.class);
List multipartFiles = multipartRequest.getFiles("modifFile");
if(!StringUtils.isBlank(multipartFiles.get(0).getOriginalFilename())){
//1 文件保存
String[] filePaths = uploadFile(multipartFiles.get(0));
}
//封装渠道信息
ChannelRequest request = packaging(channelId, status, showFlag, flag,
channelName, fileParserName, fileCompareName, importorName,
rule, sourceConfig, schemaInfo, processors);
channelService.update(DatasoureFlagEnum.DATA_R_CASH.getCode(),request);
log.info("更新渠道成功");
return Result.success("更新渠道成功");
}catch(Exception e){
log.error("更新渠道失败,{}", e);
return Result.error("更新渠道失败");
}
}
private String[] uploadFile(MultipartFile file1) throws Exception {
if (null == file1 || file1.isEmpty()) {
throw new Exception("");
}
if (!this.validateFile(file1, FileSuffixEnum.JAR,FileSuffixEnum.ZIP)) {
throw new Exception("文件需为 jar, zip类型");
}
String origFileName = file1.getOriginalFilename();
/*String dataStr = DateUtil.getCurDate();
String chargeFileName=origFileName+"("+dataStr+")";
String fileName = UUID.randomUUID().toString().replaceAll("-", "");
fileName = fileName + origFileName.substring(origFileName.lastIndexOf('.'), origFileName.length());*/
String filePath1 = batchContentService.channelUploadFile(file1.getBytes(), origFileName);
return new String[]{filePath1,origFileName};
}
/**
* @description 校验上传文件的后缀
* @param file
* @param enums
* @return
*/
protected boolean validateFile(MultipartFile file, FileSuffixEnum... enums) {
try {
if (file == null || file.isEmpty()) {
return false;
}
for (FileSuffixEnum t : enums) {
if (StringUtils.equals(t.name().toUpperCase()
, FilenameUtils.getExtension(file.getOriginalFilename()).toUpperCase())) {
return true;
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return false;
}
Service
@Override
public String channelUploadFile(byte[] data, String fileName) {
String fileDir=null;
try {
//File.separator
fileDir = ConfigFiles.deployPath;
File file = new File(fileDir +File.separator+ fileName);
if (!file.exists()) {
// 如果存在,修改文件修改时间
//FileUtils.touch(file);
FileUtils.writeByteArrayToFile(file, data);
}else {
file.delete();
// 如果存在,修改文件修改时间
//FileUtils.touch(file);
FileUtils.writeByteArrayToFile(file, data);
}
} catch (Exception e) {
LOGGER.error("文件上传失败", e);
throw new RuntimeException("文件上传失败");
}
return fileDir + File.separator + fileName;
}