@[TOC]java图片上传
前端的图片上传后台储存,返回一个映射路径:
首先,用到的jar包有
commons-beanutils-1.8.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.2.jar
下载链接:https://download.csdn.net/download/qq_41411384/11990854
在前端要有标签:
<input id="file_upload" type="file"/> <!-- 设置input的为文件上传 -->
<img src="" alt="" id="showImg"></img> <!-- 用于图片的回显 -->
方法一:自定义的方法
前端代码:
$("#file_upload").change(function(){
//this.files[0]: 获取元素对象
//通过传入元素对象,创建url路径
var url = window.URL.createObjectURL(this.files[0]);
$("#showImg").attr("src",url);
$("#showImg").css("width","50px");
$("#showImg").css("height","40px");
})
后端代码
/*用实例存储前端传过来的数据*/
//1.实例化DiskFileItemFactory对象
DiskFileItemFactory factory = new DiskFileItemFactory()