/**
* 文件上传
*/
--------------------------------controller------------------------------------------------------------
@RequestMapping(value = "/dataUploadOne", method = RequestMethod.POST)
public
@ResponseBody
JsonResultProtocol dataUploadOne(@RequestParam(value = "file", required = false) MultipartFile file,
@RequestParam("userId") Long userId,
@RequestParam("processInstanceId") String processInstanceId,
HttpServletRequest request) {
logger.debug("sessionId"+request.getSession().getId());
JsonResultProtocol result = new JsonResultProtocol();
if (file == null || file.isEmpty()) {
return result.setup(ResultCode.INVALID_PARAM);
}
return result.setup(ResultCode.SUCCESS, creditFileService.saveIdImg(file, userId, rootPath, idCardImg,processInstanceId));
}
------------------------------ajax--------------------------------------------------------------------
function doUpload() {
var formData = new FormData($("#uploadForm1")[0]);
formData.append("userId", myUser.historyId);
formData.append("processInstanceId",myProcessInstanceId);
$.ajax({
url: 'approval/dataUploadOne',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
console.log(returndata);
getCreditFileList(myProcessInstanceId);
},
error: function (returndata) {
console.log(returndata);
}
});
}
------------------------------------html--------------------------------------------------------
<form id="uploadForm1" enctype="multipart/form-data">
<input type="file" id="file" name="file"/>
<button type="button" id="" style="width:100px;height: 30px;background-color: #3c8b3c;" οnclick="doUpload()">上传资料</button>
</form>
-------------------------------------service-------------------------------------------------
//上传图片 然后保存 CreditFile
public String saveIdImg(MultipartFile files, Long userId, String rootPath, String storePath,String processInstanceId) {
FileOutputStream fos;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String str = "_" + sdf.format(date);
String suffix;
try {
String fileName = files.getOriginalFilename();
int pointIndex = fileName.lastIndexOf(".");
if (!fileName.substring(pointIndex, fileName.length()).equals(".png") && (!fileName.substring(pointIndex, fileName.length()).equals(".jpg"))) {
return null;
}
suffix = storePath + userId + str + "_file_" + fileName;
if(ALIYUN_OSS_UPLOAD) {
aliyunOSSService.uploadToOSS(files.getInputStream(), suffix);
} else {
String filePath = rootPath + storePath;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
String path = rootPath + suffix;
files.transferTo(new File(path));
}
CreditFileEntity creditFileEntity = new CreditFileEntity();
creditFileEntity.setLoanRecordId(getLoanRecordId(processInstanceId));
creditFileEntity.setFilePath(suffix);
creditFileEntity.setUploadTimestamp(new Date());
saveCreditEntity(creditFileEntity);
} catch (IOException e) {
throw new ServiceException(10009,"文件上传失败");
}
return suffix;
}
----------------------------------------------------------------------------------------------