这种不适合ajax操作 ,只适合单张图片的上传
//活动图片下部图片上传
function subFile2(){
var file = document.getElementById('file3');
if (file.value == "") {
alert("请选择您需要上传的背景图片!");
}else{
var plantId=$(".plantId").val();
var selVal= $(".activityTypeId").val();
var activityId=$(".activityId").val();
var packageId=$(".packageId").val();
var f = document.getElementsByTagName("form")[0];
var packageAmount=$(".packageAmount").val();
f.action=f.action+"&plantId="+plantId+"&selVal="+selVal+"&activityId="+activityId+"&packageAmount="+packageAmount;
f.submit();
top.closeMD(true);
}
}
后台代码
//转型为MultipartHttpRequest(重点的所在)
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得第1张图片(根据前台的name名称得到上传的文件)
MultipartFile multipartFile =null;
if("1".equals(String.valueOf(activityTypeId))){
multipartFile = multipartRequest.getFile("aa");
}else if("2".equals(String.valueOf(activityTypeId))){
if("1".equals(uploadPhotoType)){
multipartFile = multipartRequest.getFile("bb");
}else{
multipartFile = multipartRequest.getFile("cc");
}
}
byte[] bytes = multipartFile.getBytes();
String fileExt = "";// 后缀名
String oldName = "";// 原文件名称
String name = multipartFile.getOriginalFilename();
// 扩展名格式:
int lastIndex = name.lastIndexOf(".");
if (lastIndex >= 0) {
fileExt = name.substring(lastIndex + 1);
oldName = name.substring(0, lastIndex);
}
String userRealPath=null;//真正路径
File originalfile = null;
String serverRealPath = SystemConfiguration.getImageServerRealPath(ado);//真正的路径
String domain = SystemConfiguration.getImageDomain(ado);
if("1".equals(String.valueOf(activityTypeId))){
userRealPath = "/images/plantActivity/"+plantId+"/"+activityTypeId+"/"+oldName+"."+fileExt;// 上传活动背景图片的路径
}else if("2".equals(String.valueOf(activityTypeId))){
if("1".equals(uploadPhotoType)){
userRealPath = "/images/plantActivity/"+plantId+"/"+activityTypeId+"/topImg."+fileExt;//上传活动背景上部图片的路径
}else{
userRealPath="/images/plantActivity/"+plantId+"/"+activityTypeId+"/activeImg1."+fileExt;//套餐图片路径
}
}
File dir = new File(serverRealPath + userRealPath);
if (!dir.exists()) {
dir.mkdirs();
}
// 生成文件名:serverRealPath + userRealPath
originalfile = new File(serverRealPath + userRealPath);
try {
multipartFile.transferTo(originalfile);// 写到磁盘