详解SpringMVC实现图片上传以及该注意的小细节

作者:JustCode 时间:2022-04-29 08:34:30 

先附上图片上传的代码

jsp代码如下:


<form action="${path}/upload/uploadPic.do" method="post" enctype="multipart/form-data">  
<div>
   ![](${path}/mall/image/load_image.png)
   <input type="file" id="input-image" name="input-image">
   <input id="input-relative-path" name="imgs" type="hidden" >
   <input id="input-last-path" type="hidden">
   <input type="submit" value="上传图片">
</div>
</form>

controller代码:通过spring的方式实现


@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController {
 @RequestMapping(value = "/uploadPic", method = RequestMethod.POST)
 @LoginCheck
 public void uploadPic(HttpServletRequest request, PrintWriter out, String lastRealPath) throws IOException {
   // 将当前上下文初始化给CommonsMultipartResolver
   CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
   // 检查form中是否有enctype="multipart/form-data"
   if (resolver.isMultipart(request)) {
     // 强制转化request
     MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
     // 从表单获取input名称
     Iterator<String> iterable = req.getFileNames();
     // 存在文件
     if (iterable.hasNext()) {
       String inputName = iterable.next();
       // 获得文件
       MultipartFile mf = req.getFile(inputName);
       byte[] mfs = mf.getBytes();
       // 定义文件名
       String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
       Random random = new Random();
       for (int i = 0; i < 3; i++) {
         fileName = fileName + random.nextInt(10);
       }
       // 获得后缀名
       String oriFileName = mf.getOriginalFilename();
       String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));

// 上传图片到本地
       String localPath = "/Users/ZR/Desktop/webPro/console/src/main/webapp/image/" + fileName + suffix;
       mf.transferTo(new File(localPath));

// 获取图片的宽高
       BufferedImage bufferedImage = ImageIO.read(new FileInputStream(new File(localPath)));
       int width = bufferedImage.getWidth();
       int height = bufferedImage.getHeight();
       // 获取文件大小
       long size = mf.getSize();
     }
   }
 }
}

spring-mvc.xml代码:


<!--
 文件上传的视图解析器,id值是固定的
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="maxUploadSize" value="1024000"/>
 <!-- 其他属性 -->
</bean>

功能的实现其实很简单,但是对于初学者还是需要注意如下几个点

  • form上的enctype="multipart/form-data"不能忘记。

  • <input type="file" onchange="submitUpload()" id="input-image" name="input-image"> 的name标签可以随便取名,但是不能忽略,否则Iterator<String> iterable = req.getFileNames();这边获取的集合将为空。

来源:http://www.jianshu.com/p/269206b48e9d

标签:spring,mvc,上传
0
投稿

猜你喜欢

  • java简单坦克大战制作代码

    2023-02-07 05:08:37
  • java中 ${} 和 #{} 有什么区别

    2023-11-29 01:34:32
  • TKmybatis的框架介绍和原理解析

    2022-08-28 21:35:42
  • springboot整合Quartz实现动态配置定时任务的方法

    2023-03-08 22:13:10
  • 一文带你学会Java事件机制

    2022-07-22 05:05:11
  • Java多线程下载文件实现案例详解

    2023-11-09 14:02:36
  • mybatis注解与xml常用语句汇总

    2022-05-17 18:39:47
  • Java的Spring框架下的AOP编程模式示例

    2023-11-02 00:52:25
  • Java线程池ThreadPoolExecutor源码深入分析

    2023-11-09 19:49:36
  • Spring事务传播中嵌套调用实现方法详细介绍

    2021-08-31 22:34:24
  • Flutter状态管理Bloc使用示例详解

    2023-08-24 09:09:10
  • Springboot配置文件内容加密代码实例

    2022-09-13 05:56:09
  • Java Condition条件变量提高线程通信效率

    2022-11-26 13:32:46
  • SpringBoot整合Shiro两种方式(总结)

    2021-09-08 21:28:24
  • C# 获取当前年份的周期及周期所在日期范围(推荐)

    2021-10-06 15:00:44
  • SpringBoot选择自有bean优先加载实现方法

    2023-05-21 06:22:39
  • 教你怎么用Idea打包jar包

    2023-03-15 03:30:51
  • ShardingSphere jdbc实现分库分表核心概念详解

    2023-11-24 12:09:45
  • JavaMail实现邮件发送的方法

    2023-08-18 06:37:38
  • 解决idea check out 切换分支时找不到需要的分支问题

    2023-04-04 09:12:57
  • asp之家 软件编程 m.aspxhome.com