后台接收前端文件图片

前端代码为:

<form action="/userSave" method="post" enctype ="multipart/form-data">
                        用户名:<input type="text" name="userName" autofocus required placeholder="NAME">
                       
                        密码:<input type="password" name="userPwd" autofocus required placeholder="PASSWORD">
                       
                        邮箱:<input type="email" name="userEmail" autofocus required placeholder="Email,Address">
                      
                        选择头像:<input type="file" name="file" id="file">
                       
                        <input type="submit" value="注册" style="color: white; background: black;font-size: 20px">
                  

                    </form>

后端代码:

//上传保存课程图片,上传课程
    @RequestMapping(value = "/CourseSave",method = RequestMethod.POST)
    //上传的feil参数    传到这里
    public String addCourse(@RequestParam(value = "file",required = false) MultipartFile file
            , CourseTable table) throws IOException {
        //图片上传成功后,将图片的地址写到数据库D://images
        String filePath="D://images";
        File pathFile=new File(filePath);
        if(pathFile.exists()){
            pathFile.mkdirs();
        }

        //获得原始图片的拓展名
        String originalFileName=file.getOriginalFilename();
        //新的文件名字
        String newfileName= UUID.randomUUID()+originalFileName;
        //封装上传文件位置的全路径
        File targetFile=new File(filePath,newfileName);
        //把本地文件上传到封装上传文件位置的全路径
        file.transferTo(targetFile);
        //设置图片路径,为字符串类型
        table.setCoursePic(newfileName);
        Service.insertCourse(table);

        return "index";
    }

配置文件中需要更改的内容为:



# 数据库连接驱动,4大金刚
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/bolg?characterEncoding=UTF-8
    username: root
    password: root123
    driver-class-name: com.mysql.cj.jdbc.Driver
#mapper xml 文件扫描
  mybatis:
    typeAliasesPackage: com.bolgs.pojo
    mapperLocations: classpath:mapper/*.xml
#    分页依赖
    hepagehelper:

      lperDialect: mysql

      reasonable: true

      supportMethodsArguments: true

      params: count=countSql
#  mvc 视图解析器
  mvc:
    view:
      prefix: /templates/*
      suffix: .
      static-location: assets/**
#  web 网页静态资源加载路径
  web:
    resources:
      static-locations: file:d:/images,classpath:/META-INF/resources/,classpath:/resources/,classpath:static/,classpath:/public/

server:
  port: 8080

servlet:
  session:
    timeout: 10s

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

能像风一样

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值