java使用spring+springmvc+mybatis上传图片

需要导入jar包,如图





----------------------jsp页面----------------------

enctype="multipart/form-data" method="post":form表单里面必须写,
必须是post方式提交
<TR>
<td>文件上传:</td>
<td>
<input type="file" name="upload" />
</td>
</TR>


----------------------如果是html 页面----------------------
<div id="importWindow" class="easyui-window" title="导入" 
style="width: 200px;height: 100px" data-options="closed:true">
<form id="importForm" enctype="multipart/form-data">
导入文件:<input type="file" name="upload" />
<button type="button" id="btnImport">导入</button>
</form>
</div>


*******点击按钮********************************
$("#btnImport").click(function(){
$.ajax({
url:"supplier_doImport.action",
dataType:"json",
type:"post",
data:new FormData( $("#importForm")[0] ),
processData:false,//必须要添加
contentType:false,//必须要添加该参数
success:function(value){
$.messager.alert('提示',value.message); 
if(value.success){
$('#importWindow').window('close');
//2、刷新数据
$("#grid").datagrid('reload');
}
}
});
})


----------------------action页面----------------------
//表示文件上传的文件
private File upload;
//上传文件的MIME类型
private String uploadContentType;
//表示上传文件的名称
private String uploadFileName;
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}




----------------------service页面----------------------
//上传文件
public void save(Customer customer, File upload, String uploadFileName) throws IOException {
//文件上传
//判断,用户是否选择了文件上传
if(uploadFileName != null){
//先把名称换成唯一的值
String uuidName = UploadUtils.getUUIDName(uploadFileName);
//使用唯一名称作为文件的名称
String filepath = "G:\\JavaEE(ChuanZhi)\\apache-tomcat-7.0.52\\webapps\\file\\"+uuidName;
//创建目标文件
File destFile = new File(filepath);
FileUtils.copyFile(upload, destFile);

// 给客户对象属性赋值
customer.setFilename(uploadFileName);
customer.setFilepath(filepath);
}
customerDao.save(customer);
}






修改上传的文件


/*
* 修改客户,同时上传文件
*/
public void update(Customer customer, File upload, String uploadFileName) throws Exception {
//处理文件上传
if(uploadFileName != null){
//删除原有文件
String oldFilepath = customer.getFilepath();
if(oldFilepath != null){
//创建文件
File oldFile = new File(oldFilepath);
oldFile.delete();
}
//上传新的文件
String uuidName = UploadUtils.getUUIDName(uploadFileName);
String filePath = "G:\\JavaEE(ChuanZhi)\\apache-tomcat-7.0.52\\webapps\\file\\"+uuidName;
File newFile = new File(filePath);

FileUtils.copyFile(upload, newFile);

customer.setFilename(uploadFileName);
customer.setFilepath(filePath);
}

customerDao.update(customer);

}
========================springmvc方式上传文件=================================
(1)controLler类中添加属性
MultipartFile pictureFile(pictureFile该参数必须和前端的name值一样)
(2)service层:
//处理文件上传
String uploadFileName = pictureFile.getOriginalFilename();
if(uploadFileName != null){
//删除原有文件
String oldFilepath = items.getPic();
if(oldFilepath != null){
//创建文件
File oldFile = new File(oldFilepath);
oldFile.delete();
}
//上传新的文件
String uuidName = UploadUtils.getUUIDName(uploadFileName);
String filePath = "G:\\apache-tomcat-7.0.52\\webapps\\file\\"+uuidName;
File newFile = new File(filePath);
items.setPic(filePath);




// 测试从Base64编码转换为图片文件
Base64Image.GenerateImage(base64, realPath2);


pictureFile.transferTo(newFile);//写到磁盘
}
itemsDao.updateItems(items);
(3)springmvc.xml配置:
<!-- 配置图片解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB B      1024*1024*5-->
<property name="maxUploadSize" value="5242880"/>
</bean>

-------------------图片压缩工具类-------------------
使用方式见Base64Image.java




如果不懂请联系邮箱[email protected]




猜你喜欢

转载自blog.csdn.net/ytydamotou/article/details/78410370