文件上传,html、ajax、springmvc、java

/**

* 文件上传

*/

--------------------------------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;

}

----------------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值