欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

文件上传SpringBoot后端MultipartFile参数报空问题的解决办法

程序员文章站 2022-04-25 09:43:31
最近写了一个文件上传的小demo,就是简单的前端html页面,后端controller接收,但是后端一直报错文件为null,看了很多文章,有说spring-boot自带的org.springframe...

最近写了一个文件上传的小demo,就是简单的前端html页面,后端controller接收,但是后端一直报错文件为null,看了很多文章,有说spring-boot自带的org.springframework.web.multipart.multipartfile和multipart冲突了,要在启动类中加入@enableautoconfiguration(exclue={multipartautoconfiguration.class}),有说要在multipartfile参数前加上@requestparam(“file”)以表明该参数类型是文件类型,还有说是前端表单没有设置enctype=“multipart/form-data”,参照上面方法我还是没有解决问题,后来更换了spring-boot版本就好了,代码如下:

1、pom.xml(注意我这里用的是2.0.4版本)

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.4.release</version>
  </parent>
  <groupid>org.sang</groupid>
  <artifactid>chapter01</artifactid>
  <version>1.0-snapshot</version>
  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

2、upload.html页面

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="uploadfile" value="请选择文件">
  <input type="submit" value="上传">
</form>
</body>
</html>

3、后端controller

import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.multipart.multipartfile;
import javax.servlet.http.httpservletrequest;
import java.io.file;
import java.io.ioexception;
import java.text.simpledateformat;
import java.util.date;
import java.util.uuid;

@restcontroller
public class fileuploadcontroller {
  simpledateformat sdf = new simpledateformat("yyyy/mm/dd");
  @postmapping("upload")
  public string upload(multipartfile uploadfile, httpservletrequest req){
    string realpath = req.getsession().getservletcontext().getrealpath("/uploadfile");
    string format = sdf.format(new date());
    file folder = new file(realpath+format);
    if(!folder.isdirectory()){
      folder.mkdirs();
    }
    string oldname = uploadfile.getoriginalfilename();
    string newname = uuid.randomuuid().tostring()+oldname.substring(oldname.lastindexof("."),oldname.length());
    try{
      uploadfile.transferto(new file(folder,newname));
      string path = req.getscheme()+"://"+req.getservername()+":"+req.getserverport()+"/uploadfile/"+format+newname;
      return path;
    }catch (ioexception e){
      e.printstacktrace();
    }
    return "上传失败";
  }
}

到此这篇关于文件上传springboot后端multipartfile参数报空问题的解决办法的文章就介绍到这了,更多相关springboot multipartfile参数报空 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!