环境jquery / maven:commons-fileupload-1.3.1.jar
<input id="imagePic" name="imagePic" type="file" accept=".bmp,.jpg,.png,.jpeg,image/bmp,image/jpg,image/png,image/jpeg"/>
$(document).ready(function () {
$("#imagePic").on("change", function (e) { var file = e.target.files[0]; //获取图片资源 var fileTypes = ["bmp", "jpg", "png", "jpeg"]; var bTypeMatch = false for (var i = 0; i < fileTypes.length; i++) { var start = file.name.lastIndexOf("."); var fileType = file.name.substring(start + 1); if (fileType.toLowerCase() == fileTypes[i]) { bTypeMatch = true; break; } } if (bTypeMatch) { if (file.size <= 1024 * 1024 * 10) {
var formData = new formData();
formData.append("file",file);
$.ajax({
url:url,type:'post',data:formData,contentType:false,processData:false,success:function(data){
}
});
var reader = new FileReader(); reader.readAsDataURL(file); // 读取文件 // 渲染文件 reader.onload = function (arg) { $(".imageShow").show() $("#uploadImageShow").attr("src", arg.target.result) btnUploadText = '重新上传' $("#uploadButton").text(btnUploadText) }
} else { alert('仅支持不超过10M的图片'); emptyImageUpload("#imagePic") $("#uploadImageShow").attr("src", "") $(".imageShow").hide() btnUploadText = '上传' $("#uploadButton").text(btnUploadText) return false; } } else { alert('仅限bmp,jpg,png,jpeg图片格式'); emptyImageUpload("#imagePic") $("#uploadImageShow").attr("src", "") $(".imageShow").hide() btnUploadText = '上传' $("#uploadButton").text(btnUploadText) return false; } }); })
});
controller
/**
* 文件上传
*/
@ResponseBody
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public HashMap<String,Object> fileUpload(HttpServletRequest request){
HashMap<String,Object> modelMap = new HashMap<>();
//获取文件输入流
MultipartHttpServletRequest multipartHttpServletRequest = null;
CommonsMultipartFile file = null;
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断是否上传了文件
if (resolver.isMultipart(request)){
multipartHttpServletRequest = (MultipartHttpServletRequest) request;
file = (CommonsMultipartFile) multipartHttpServletRequest.getFile("file");
//获取上传文件的原始名称
String originalFilename = file.getOriginalFilename();
//设置上传文件的保存地址目录(这里我设置在本地D盘中)
System.out.println("request.getServletContext().getRealPath("/upload"):"+request.getServletContext().getRealPath("/upload"));
String dirPath = "d:/Sakura/upload/";
//如果保存文件的地址不存在,就先创建目录
File filePath = new File(dirPath);
if (!filePath.exists()){
filePath.mkdirs();
}
//使用UUID从命名上传的文件名称
String newFilename = UUID.randomUUID() + "_" + originalFilename;
try {
//使用CommonsMultipartFile完成文件上传到指定位置
file.transferTo(new File(dirPath+newFilename));
} catch (IOException e) {
modelMap.put("success",false);
modelMap.put("errMsg",e.getMessage());
return modelMap;
}
modelMap.put("success",true);
modelMap.put("errMsg","文件上传成功");
}else {
modelMap.put("success",false);
modelMap.put("errMsg","未上传文件");
}
return modelMap;
}
————————————————
版权声明:本文为CSDN博主「鹿谷門実」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43517302/article/details/106202549