js代码:
/**
* 上传并回显轮播图
* @returns
*/
doUpload:function(){
var formData = new FormData($(“#uploadForm”)[0]);
$.ajax({
url: ‘../banner/upPic.do’ ,
type: ‘POST’,
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
banner.defaultOption.img=data.resData;
$(“#scrollPic”).attr(“src”,”../upload/”+data.resData);
},
error: function (returndata) {
console.log(returndata);
}
});
},
后台代码
@RequestMapping(value = “/headPic”)
public String headPic(HttpServletRequest request) throws Exception {
log.info(GlobalContract.LOG_BEGIN);
ResultVO resultVO = null;
//获取图片类
MultipartRequest muRequest = (MultipartRequest) request;
MultipartFile muFile = muRequest.getFile(“headPic”);
if (!muFile.isEmpty()) {
//创建文件储存路径
String uploadPath = request.getSession().getServletContext()
.getRealPath(“/upload”);
//获取原始文件名
String originalName = muFile.getOriginalFilename();
//切割保留后缀名,起新名
int beginIndex = originalName.lastIndexOf(“.”);
String suffix = originalName.substring(beginIndex);
String newName = new Date().getTime() + suffix;
//文件写入
File uploadFile = new File(uploadPath + File.separator + newName);
try {
FileUtils.copyInputStreamToFile(muFile.getInputStream(),
uploadFile);
resultVO=new ResultVO();
//返回新文件名
resultVO.setResData(newName);
} catch (IOException e) {
e.printStackTrace();
resultVO = new ResultVO(GlobalMessage.MSG_01);
log.error(“错误参数:” + originalName);
log.error(“错误信息:” + e.getMessage());
}
} else {
resultVO=new ResultVO();
resultVO.setResData(“defeat”);
}
log.info(GlobalContract.LOG_END);
return JSONObject.fromObject(resultVO).toString();
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13905.html