<form id="showDataForm" class="layui-form" enctype="multipart/form-data" onsubmit="return PostData()">
二、用ajax提交
$.ajax({
type: "POST",
url: '',
data: formData,
timeout: 6000,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (msg) {
setTimeout(function () {
$("#scbtn").attr("disabled", false);//将提交按钮解除为可点击
layer.msg('提交成功', {
time: 1000
}, function () {
window.location.href = "xx.html";
});
},1000);
},
error: function (msg) {
layer.msg('提交超时,请检查您的网络连接!');
$("#scbtn").attr("disabled", false);
}
});
return false;
3、后台接收代码
private File file;//与前端name一致
private String fileFileName;//接收的名字
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public static int getSecondTimestamp(Date date){//将上传的图片名字改为yy-mm-dd格式
if (null == date) {
return 0;
}
String timestamp = String.valueOf(date.getTime());
int length = timestamp.length();
if (length > 3) {
return Integer.valueOf(timestamp.substring(0,length-3));
} else {
return 0;
}
}
public String add() throws Exception {
Date date=new Date();
if(file != null){
String path = ServletActionContext.getServletContext().getRealPath("/upload");//服务器项目根路径
File filePath = new File(path);
if(!filePath.exists()){
filePath.mkdir();
}
String format = fileFileName.split("\\.")[1];
File diskFile = new File(path + File.separator
+ String.valueOf(getSecondTimestamp(date))+"."+format);
FileUtils.copyFile(file, diskFile);
casCase.setFurImage(String.valueOf(getSecondTimestamp(date))+"."+format);//图片名字
}
}