1. 在工程依赖库下添加文件上传jar包

commons-fileupload-1.2.2.jar

commons-io-2.4.jar

2.jsp页面设置form表单属性enctype

在表单中上传图片时,需要在form的属性设置中添加enctype="multipart/form-data"。

[html] view plain copy

<form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" enctype="multipart/form-data" >

表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作. enctype="multipart/form-data"是上传二进制数据;form里面的input的值以2进制的方式传过去。

  1. springMVC.xml添加multipart类型解析器

在页面form中提交enctype="multipart/form-data"的数据时,需要springmvc对multipart类型的数据进行解析,需要在springmvc.xml中配置multipart类型解析器。

[html] view plain copy

<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>

4.创建图片虚拟目录,以存放图片

eclipse IDE 通过界面进行配置:servers -->Tomcat v8.0 Server at localhost--> 双击,选择-->add external web modules.

在图片虚拟目录中,一定将图片目录分级创建(提高i/o性能),一般我们采用按日期(年、月、日)进行分级创建。

  1. jsp 页面,设置图片显示的位置和大小。

[html] view plain copy

<tr>
<td>商品图片</td>
<td>
<c:if test="${itemsCustom.pic !=null}">
<img src="/pic/${itemsCustom.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file" name="items_pic"/>
</td>
lt;/tr>
  1. Controller 类方法中写相应的方法

<1, 方法的参数中添加MultipartFile items_pic行参 这个跟页面的图片的参数名字是一致的;

<2, 为了避免文件名称相同而冲突,使用UUID.randomUUID()产生一个随机的数值作为名称;

❤️. 将图片数据写入磁盘:items_pic.transferTo(newFile);

<4. 更新itemsCustom中属性pic的值itemsCustom.setPic(newFileName);

[java] view plain copy

    //在需要校验的poJo类前加 @Validated, 后面加BindingResult bindingResult 存放出错信息。
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model,
HttpServletRequest request,Integer id,
@Validated ItemsCustom itemsCustom,
BindingResult bindingResult,
MultipartFile items_pic)throws Exception { if(bindingResult.hasErrors()){
List<ObjectError> allErrors = bindingResult.getAllErrors();
for(ObjectError objectError :allErrors){
System.out.println(objectError.getDefaultMessage());
} model.addAttribute("allErrors", allErrors);
model.addAttribute("itemsCustom", itemsCustom);
return "items/editItems";
} //原始名称
String originalFilename = items_pic.getOriginalFilename();
//上传图片
if(items_pic!=null && originalFilename!=null && originalFilename.length()>0){
//存储图片的物理路径
String pic_path = "C:\\Users\\Administrator.MICROSO-U8JSS8B\\Desktop\\java_code\\picture\\";
//新的图片名称
String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
//新图片
File newFile = new File(pic_path+newFileName);
//将内存中的数据写入磁盘
items_pic.transferTo(newFile);
//将新图片名称写到itemsCustom中
itemsCustom.setPic(newFileName); }
itemsService.updateItems(id, itemsCustom); // return "success";
return "forward:queryItems.action";
}

7.测试效果

参考:http://zkliqiang.iteye.com/blog/779285

springMVC图片文件上传功能的实现的更多相关文章

  1. springmvc图片文件上传接口

    springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller; import java.awt.image.Buffer ...

  2. php+原生ajax实现图片文件上传功能实例

    html+js 代码 <!DOCTYPE html> <html> <head> <title>Html5 Ajax 上传文件</title> ...

  3. 16.SpringMVC核心技术-文件上传

    上传单个文件 1.定义具有文件上传功能的页面 index.jsp,其表单的设置需要注意,method 属性为 POST, enctype 属性为 multipart/form-data.另外,需要注意 ...

  4. SpringMvc MultipartFile 图片文件上传

    spring-servlet.xml <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipar ...

  5. SpringMVC学习--文件上传

    简介 文件上传是web开发中常见的需求之一,springMVC将文件上传进行了集成,可以方便快捷的进行开发. springmvc中对多部件类型解析 在 页面form中提交enctype="m ...

  6. iOS 的 Safari 文件上传功能详解

    iOS 6 给 Safari 浏览器带来的另外一个功能是文件上传,终于 Safari 终于支持 input 输入框的文件类型了,并且还支持 HTML媒体捕获(HTML Media Capture). ...

  7. SpringMVC+ajax文件上传实例教程

    原文地址:https://blog.csdn.net/weixin_41092717/article/details/81080152 文件上传文件上传是项目开发中最常见的功能.为了能上传文件,必须将 ...

  8. 使用element的upload组件实现一个完整的文件上传功能(下)

    本篇文章是<使用element的upload组件实现一个完整的文件上传功能(上)>的续篇. 话不多说,接着上一篇直接开始 一.功能完善—保存表格中每一列的文件列表状态 1.思路 保存表格中 ...

  9. 【SpringMVC】SpringMVC 实现文件上传

    SpringMVC 实现文件上传 文章源码 文件上传回顾 查看 JavaWeb 阶段的文件上传下载 实现步骤: 客户端: 发送 post 请求,告诉服务器要上传什么文件 服务器: 要有一个 form ...

随机推荐

  1. Spring3系列5-Bean的基本用法

    Spring3系列5-Bean的基本用法 本篇讲述了Bean的基本配置方法,以及Spring中怎样运用Bean. 主要内容如下: 一.      Spring中Bean的相互引用 二.      Sp ...

  2. sql2008清空日志

    USE[master] GO ALTER DATABASE MeSizeSNS SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE MeSizeSNS ...

  3. dissmiss a UISearchBar with an SearchBarController

    If you want do dissmiss a UISearchBar with an SearchBarController, just use this Code: [self.searchD ...

  4. ffmpeg 中 swscale 的用法

    http://www.guguclock.com/2009/12/ffmpeg-swscale.html 如果想將某個PixelFormat轉換至另一個PixelFormat,例如,將YUV420P轉 ...

  5. MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)

    转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...

  6. tcp/ip协议listen函数中backlog参数的含义与php-fpm的502 Bad Gateway

    To understand the backlog argument, we must realize that for a given listening socket, the kernel ma ...

  7. 配置svn

    Linux搭建SVN Server 其中包括添加防火墙规则

  8. SQLServer的数据类型

    第一大类:整数数据 bit:bit数据类型代表0,1或NULL,就是表示true,false.占用1byte.int:以4个字节来存储正负数.可存储范围为:-2^31至2^31-1.smallint: ...

  9. Unity3D Android手机开发环境配置,可真机发布调试

    此方法配置好,在可以在unity直接发布到手机上,并可以实时调试. 1.配置eclipse环境:首先在官网下载安装包:http://developer.android.com/sdk/index.ht ...

  10. codeforces George and Job

    /* 题意:给一个长度为n的序列, 从中选择长度为m的k个区间(任意两个区间不会有公共部分) 使得所选择的区间的和最大! 思路:这是一种很常见的dp dp[i][j] 表示的是前 i 个数选择 j 个 ...