上传图片

上 传 图 片 功 能

1.首先 pom.xml 里面引入依赖
在这里插入图片描述

       <!-- 文件上传组件 -->
     <dependency>
         <groupId>commons-fileupload</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>1.3.1</version>
     </dependency>
 </dependencies>
  1. 配置文件里面设置一下上传文件大小

在这里插入图片描述

 <!-- 上传拦截,如最大上传值及最小上传值 -->
 <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
       <property name="maxUploadSize">
              <value>2097152</value>
       </property>
       <property name="maxInMemorySize">
              <value>4096</value>
       </property>
       <property name="defaultEncoding">
              <value>utf-8</value>
       </property>
 </bean>

<< 在前台又一个name往里面传一个文件的路径>>
在这里插入图片描述
在from表单里面必须用post 还必须设置 enctype
在这里插入图片描述
name = “file” 将file传到后台 定义一下参数 MultipartFile file
《《在列表页面需要显示的是》》
在这里插入图片描述
数据库里面logo 这个字段是用来写图片存放的路径的

  1. 在控制层里面写传入的文件名 controller 控制层写入传的方法

在这里插入图片描述

//获取原始的名字
  String originalFilename = file.getOriginalFilename();
  //获取图片路径的后缀
  String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
  //获取绝对路径
  String path = request.getSession().getServletContext().getRealPath("/");
  //uuid 随机生成 一个新的名字
  String str = UUID.randomUUID().toString()+substring;
  //文件路径
  File f = new File(path+"upload/"+str);
  //传存放照片的文件 创建一个新的文件夹 getParentFile 是获取文件的名字
  if(!f.getParentFile().exists()) {
   f.getParentFile().mkdirs();
  }
  //转存文件
  try {
   file.transferTo(f);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //这里记住要把文件传进去  set它里面的属性
  String logo="http://localhost:8080/upload/"+str;
  /**
  *  现在目前只有这个http://localhost:8080/upload/  才可以传进去  而***E:\workspace\spring_boot_eclipse\zk2_12.10\src\main\webapp\  获取的绝对路径进不去
  * 现在知道了 配置绝对路径的话 需要在tomcat里面配置虚拟路径 而我的这个tomcat用的是插件
  * 我这个上传图片只是暂时的直接将它放在了webapp下面 用localhost:8080是用来直接访问它项目的
  * 我这个图片直接上传 就上传到 项目里面 了
**/
  record.setLogo(logo);

需要导包

import org.springframework.web.multipart.MultipartFile;

这个包是 spring里面的

需要早pom.xml 里面引入依赖

<!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.10.RELEASE</version>
        </dependency>

xml

表头

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       ">
   </beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值