【问题标题】:org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was foundorg.apache.tomcat.util.http.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界
【发布时间】:2020-08-27 18:39:30
【问题描述】:

我正在构建一个小的 springboot 应用程序,它在 Neo4j 图形数据库上进行 CRUD 操作。其中一项操作是使用 CSV 文件批量插入。

我收到以下错误: 来自铬

Access to XMLHttpRequest at 'http://localhost:9090/neo4jtest/api/employees/upload-csv' from origin 'http://localhost:8090' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

这是控制器(我删除了无用的映射):

@RestController
@RequestMapping(path = "/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/upload-csv")
    public String uploadCSVFile(@RequestParam("file") MultipartFile file, Model model) {

        // validate file
        if (file.isEmpty()) {
            model.addAttribute("message", "Please select a CSV file to upload.");
            model.addAttribute("status", false);
        } else {
            // parse CSV file to create a list of `employee` objects
            try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
                // create csv bean reader
                CsvToBean<Employee> csvToBean = new CsvToBeanBuilder<Employee>(reader).withType(Employee.class).withIgnoreLeadingWhiteSpace(true).build();
                // convert `CsvToBean` object to list of employees
                // TODO : save employees into neo4j
                // save employees list on model
                model.addAttribute("employees", Employees.builder().employeeList(csvToBean.parse()).build());
            } catch (Exception ex) {
                model.addAttribute("message", "An error occurred while processing the CSV file.");
                model.addAttribute("status", false);
            }
        }   
        return "list";
    }
}

这是使用 thymleaf 模板的网页:

<script>
$(document).ready(function() {
    $("#btn-upload-csv").click(function() {
        $('#neo4j-filechooser').trigger('click');
    });

    $('#neo4j-filechooser').on('change', function() {
        myfiles = this.files; //save selected files to the array
        console.log(myfiles[0]); //show them on console
        formdata = new FormData();
        formdata.append("file",myfiles[0])
        $.ajax({
            url: "http://localhost:9090/neo4jtest/api/employees/upload-csv",
            type: "POST",
            processData: false,
            contentType: false,
            headers: {
                "Content-Type": "multipart/form-data",
                "Access-Control-Allow-Origin":"*",
                "Access-Control-Allow-Methods": "POST",
                "Access-Control-Allow-Headers": "Content-Type, Authorization"
            },
            data: formdata
        }).then(function(data) {
           $('.greeting-id').append(data.id);
           $('.greeting-content').append(data.content);
        });
    }).click();
});

感谢任何帮助。谢谢。

【问题讨论】:

    标签: spring-boot tomcat file-upload neo4j


    【解决方案1】:

    从您的错误堆栈跟踪可以看出,错误实际上是:

    org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
    

    即使设置了内容类型,也没有设置多部分边界。所以,问题是你自己设置了Content-Type,让它为空,以便 jquery 为你做。根据规范,问题实际上出在您的请求中:

    多部分实体的 Content-Type 字段需要一个参数, “boundary”,用于指定封装边界。这 封装边界被定义为一条完全由两个组成的线 连字符(“-”,十进制代码 45)后跟边界 Content-Type 标头字段中的参数值。

    在您的 jquery 请求中,您正在设置 processData: false, 和contentType: false,它强制jquery 设置内容类型和边界。但是在请求的标题部分,您再次手动添加content-type。从代码中删除它并尝试。如果它不起作用,请您分享示例数据,以便我们进行更多分析。如果您使用的是旧版本的 jquery 或从旧浏览器运行它,也可能会出现此问题。

    试试下面的代码:

    <script>
    $(document).ready(function() {
        $("#btn-upload-csv").click(function() {
            $('#neo4j-filechooser').trigger('click');
        });
    
        $('#neo4j-filechooser').on('change', function() {
            myfiles = this.files; //save selected files to the array
            console.log(myfiles[0]); //show them on console
            formdata = new FormData();
            formdata.append("file",myfiles[0])
            $.ajax({
                url: "http://localhost:9090/neo4jtest/api/employees/upload-csv",
                type: "POST",
                processData: false,
                contentType: false,
                headers: {
                    "Access-Control-Allow-Origin":"*",
                    "Access-Control-Allow-Methods": "POST",
                    "Access-Control-Allow-Headers": "Content-Type, Authorization"
                },
                data: formdata
            }).then(function(data) {
               $('.greeting-id').append(data.id);
               $('.greeting-content').append(data.content);
            });
        }).click();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2013-07-02
      • 2019-03-18
      • 1970-01-01
      • 2013-05-10
      • 2015-09-14
      • 2018-01-26
      相关资源
      最近更新 更多