前端
<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";
}
大佬勿喷,欢迎提意见建议评论!!!!