具体在service层和controller层该如何写呢,请求详解~
http://blog.csdn.net/qq_34309305/article/details/66968584
这里实现了图片上传的功能,还有原代码
建议用插件比如uploadFile,都有教程,功能强大,还是经过多次测试,性能稳定的
前段时间刚做的,写成了博客,不懂的可以交流:http://blog.csdn.net/xuanzhangran/article/details/54928997
http://blog.csdn.net/u013772876/article/details/68488737
可以用FormData来上传图片;
@ResponseBody
@RequestMapping(value = "/updateCamPic.html", method = RequestMethod.POST)
public String updateCamPic(@RequestParam(value = "file", required = false) MultipartFile file,String ipAddress) {
MultipartFile cf = file;
String name = cf.getOriginalFilename();
try {
name = ipAddress.replace("\\.", "")+name;
String filePath = this.getRequest().getSession().getServletContext().getRealPath("/");
FileUtils.writeByteArrayToFile(new File(filePath + "//images//" +name), cf.getBytes());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return name;
}
function updateCamPic(ipAddress) {
var fileObjA = document.getElementById("fileA").files[0];
var formData = new FormData();
formData.append("file", fileObjA);
formData.append("ipAddress", ipAddress);
$("#btnSubmit").attr("disabled", "disabled");
$.ajax({
type: 'POST',
async: false,
url: 'updateCamPic.html',
data: formData,
contentType: false,
processData: false,
success: function (data) {
$("#picurl").val(data);
},
error: function (a, b, c) {
if (a.status == 404) {
alert("404");
}
else if (a.status != 0) {
alert(a.status + ":" + a.responseJSON.Message);
}
}
}).fail(function (a,b,c) {
alert("接口调用失败"+a.responseJSON.description)
}).complete(function () {
$("#btnSubmit").removeAttr("disabled");
});
}
上传照片:</label><div class="formControls col-3"><input type="file" name="fileA" id="fileA" accept=".gif,.jpg,.jpeg,.png" /></div>
使用multipartfile 上传
http://blog.csdn.net/one_isi_all/article/details/46610089 jsp页面通过ajax上传多张图片