ajax+springboot文件上传

ajax+springboot文件上传

前端

<form id="addForm" enctype="multipart/form-data">
								<input class="form-control" type="file" name="files" style="height: 35px;">
								<input id="solution" name="solution" class="form-control" type="text" style="">
								<button type="submit" class="btn btn-primary" onclick="scfile()" style="margin:2%">上传</button>
							</form>
function scfile(){
		var formData = new FormData($("#addForm")[0]);
 		$.ajax({
 			url: "${ctx}/PromeasureList/loadfile", // 文件上传接口
 			type:'post',
 			data:formData,
 			processData: false,
 			contentType:false,
 			success:function (data) {
				$.ajax({
					url:"${ctx}/PromeasureList/updatepath",
					type:"post",
					data:{
						"mainId":$("#solution").val(),
						"daliantype":daliantype,
						"filepath":data
					},
					async:false,
					success:function(data){
						if(data == "200"){
							$("#myModal4").modal("hide");
							load();
						}
					}
				})
 			},
 			error:function () { }
 		});
 	}

后端

//文件上传loadfile
	@RequestMapping(value = { "/loadfile" }, method = RequestMethod.POST)
	@ResponseBody
	public String loadfile(HttpServletRequest request, @RequestParam("files") MultipartFile files) throws IOException {
		Date date = new Date();
		String strDateFormat = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

		String fileName = sdf.format(date)+ files.getOriginalFilename();
		String filepath = "D:\\fileSource\\autopathfile\\"+fileName;
		File file = new File(filepath);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		if(!files.isEmpty()){
			byte [] bytes = files.getBytes();
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(filepath)));
			bufferedOutputStream.write(bytes);
			bufferedOutputStream.close();
		}
		return filepath;
	}


	//更新文件路径
    @RequestMapping(value = { "/updatepath" }, method = RequestMethod.POST)
    @ResponseBody
	public String updatepath(ModelMap model,HttpServletRequest request) {
    	String table_name = "dptestinfomain_"+ request.getParameter("daliantype");
    	String id = request.getParameter("mainId");
    	String filepath = request.getParameter("filepath");
    	String aaa = "UPDATE "+table_name+" SET autofilepath = '"+filepath+"' WHERE id = '"+id+"'";
    	try{
			promeasReportServ.BatchEditInfo(aaa);
		}catch (Exception e){
    		e.printStackTrace();
		}
		return "200";
	}

大佬勿喷,欢迎提意见建议评论!!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值