spring+springmvc+mybatis整合+图片上传+展示到Jsp页面

这篇博客记录了一个初学者使用Spring+SpringMVC+Mybatis整合实现图片上传并展示到JSP页面的过程。博主详细介绍了从添加依赖、配置Spring-MVC、编写SQL、DAO层、Service层,到控制器处理图片上传,以及最终在JSP页面展示图片的步骤。
摘要由CSDN通过智能技术生成

小编也是刚学习java一个月的小白,做这个图片上传和展示。百度了很多资料,就没看到有成功的,现在做出来,记录一下,同时也希望大神们给予更好的思路,小编在此深表感谢!

关于spring+springmvc+mybatis整合,这里就不介绍了,需要了解的话,请 点击了解spring+springmvc+mybatis整合

首先看看,做出来的效果图!

上传图片:

列表展示:(图片展示) 

1、pom.xml文件中添加以下2个jar包!

<!-- 
        操作文件上传
        https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload
    -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

2、在spring-mvc.xml文件中添加以下代码

<!-- 文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>

3、在mapping.xml文件中写你的SQL语句!

4、在dao层添加以下方法

5、在Service接口中添加以下方法

6、在Service接口实现类中添加以下代码

7、基类控制器,封装图片上传方法,代码如下!

/**
     * 封装图片上传方法
     * @param file            表单中的file
     * @param oldFileName    原图片名称
     * @return
     */
    protected String upload(MultipartFile file,String oldFileName) {
        if(oldFileName!=null && oldFileName.length()>0){
            // 新的图片名称
            String newFileName=UUID.randomUUID()+oldFileName.substring(oldFileName.lastIndexOf("."));
            // 拼接存放图片的物理路径
            String head_path="D:\\upload";
            File newFile=new File(head_path + &

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值