Spring Boot 文件上传错误请求 400

2024-05-03

你好,我正在使用 Spring Boot 和 AngularJs 编写一个 Web 应用程序,需要一个简单的文件上传,但目前无法正常工作。

我已经读到,当 mvc 依赖项存在时,Spring Boot 应该自动配置分段上传本身。 从 :https://spring.io/guides/gs/uploading-files/ https://spring.io/guides/gs/uploading-files/

作为自动配置 Spring MVC 的一部分,Spring Boot 将创建一个 MultipartConfigElement bean 并为文件上传做好准备。

发送请求的 Javascript 函数:

var postFormData = function (file, url, successCallback, errorCallback, progressCallback) {
    var xhr = new XMLHttpRequest(),
        formData = new FormData();

    formData.append("file", file);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                successCallback(xhr.status); //File is Uploaded
            } else {
                errorCallback(xhr.status);
            }
        }
    };

    function progress(evt) {
        if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            progressCallback(percentComplete);
        } else {
            progressCallback(evt.loaded);
        }
    }

    xhr.open("POST", url, true);

    //xhr.setRequestHeader("Content-Type", "multipart/form-data");

    xhr.addEventListener("progress", progress, false);

    xhr.send(formData);
}

我的主应用程序类中的多部分配置 Bean:

 @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(MAXIMUM_FILE_SIZE);
        factory.setMaxRequestSize(MAXIMUM_FILE_SIZE);
        return factory.createMultipartConfig();
    }

上传控制器:

@Controller
@RequestMapping("/api")
public class FileUploadController {

        @Autowired
        UserRepository userRepository;

        @Autowired
        VolumeMetaRepository volumeMetaRepository;

        /**
         * Handles File upload of volumedata
         *
         * @param file Must not be null
         **/
        @RequestMapping(value = "/volumedata/meta/test", consumes= "multipart/form-data", method=RequestMethod.POST)
        @ResponseBody
        public void handleFileUpload(
                @RequestParam("file") MultipartFile file) {

            if (!file.isEmpty()) {
               /* try {
                    InputStream fileStream = file.getInputStream();
                    OutputStream out = new FileOutputStream(file.getOriginalFilename());
                    IOUtils.copy(fileStream, out);
                } catch (IOException e) {
                    throw new IllegalStateException("Error uploading file.", e);
                }*/

/*                String filePath = request.getServletContext().getRealPath("/");
                try {
                    file.transferTo(new File(filePath+ "/"  + file.getOriginalFilename()));
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
            }
        }

因为还有其他 Stackoverflow 线程,所以新的 Spring boot 版本解决了我使用“1.2.2.BUILD-SNAPSHOT”的问题,我的 gradle 依赖项:

version = '1.2.2.BUILD-SNAPSHOT'
dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:version
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version:version
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:version
    compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version:'0.16.0.RELEASE'
    compile group: 'org.springframework.data', name: 'spring-data-rest-webmvc', version:'2.1.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version:version
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest', version:version
    compile group: 'commons-io', name: 'commons-io', version:'2.1'
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version:'2.3.4'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-integration', version:version
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version:version
    compile group: 'org.springframework.session', name: 'spring-session', version:'1.0.0.BUILD-SNAPSHOT'
    compile group: 'org.springframework.data', name: 'spring-data-redis', version:'1.4.1.RELEASE'
    compile group: 'redis.clients', name: 'jedis', version:'2.4.2'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-remote-shell', version:version
    compile group:'com.google.guava', name:'guava', version:'18.0'

    compile files ('lib/vendor/niftijio.jar');

    compile("com.h2database:h2")

    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.1.6.RELEASE') {
        exclude(module: 'commons-logging')
    }

    testCompile group: 'com.jayway.jsonpath', name: 'json-path', version:'0.9.1'
    testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
    testCompile 'junit:junit:4.+'
    testCompile "org.mockito:mockito-all:1.9.5"
}

抛出的错误是:

{
  "timestamp" : "2015-01-19T10:22:53.710Z",
  "status" : 400,
  "error" : "Bad Request",
  "exception" : "org.springframework.web.bind.MissingServletRequestParameterException",
  "message" : "Required MultipartFile parameter 'file' is not present",
  "path" : "/api/volumedata/meta/test"
}

它告诉我“文件”参数不存在,但我的请求负载显示该参数存在。

Request Payload:
------WebKitFormBoundary40qPAhpvA20pd8V1
Content-Disposition: form-data; name="file"

C:\fakepath\test.gz
------WebKitFormBoundary40qPAhpvA20pd8V1--

有人知道我的配置中缺少什么,或者什么可能导致错误?

提前致谢


我找到了错误的原因。它不是源自我的 Spring 控制器,而是源自我的 angularJS html 代码。

这是我的上传输入字段:

 <div class="form-group"
                 ng-class="{ 'has-error': volume_upload_form.file_field.$invalid && volume_upload_form.file_field.$dirty}">
                <label name=file-field-label>Volume Dataset</label>
                <input value=file
                       name=file
                       ng-model=upload.file
                       required
                       id=file
                       file_ext_validation
                       type="file"
                       extensions="nii,NII,gz,jpeg,JPG"/>

                <div class="error"
                     ng-show="volume_upload_form.volume.$invalid">

                    <small class="error"
                           ng-show="volume_upload_form.volume.$error.fileExtValidation">
                        File must be of type ".nii"
                    </small>
                </div>
            </div>

正如您所看到的,也使用默认的 ng-model 与我的角度控制器进行通信(“upload”是我的控制器别名)。

ng-model=upload.file

但Angular默认不支持文件输入html字段。因此,只有包含文件路径的字符串存储在 upload.file 中,而不是实际的文件对象。我必须编写一个自定义指令:

var fileModel = function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function () {
                scope.$apply(function () {
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}

module.exports = fileModel;

它返回实际的 File 对象。我的新上传html代码如下:

<div class="form-group"
                 ng-class="{ 'has-error': volume_upload_form.file_field.$invalid && volume_upload_form.file_field.$dirty}">
                <label name=file-field-label>Volume Dataset</label>
                <input name=file
                       file-model="upload.fileModel"
                       ng-model="file"
                       required
                       file_ext_validation
                       type="file"
                       extensions="nii,NII,gz,jpeg,JPG"/>

                <div class="error"
                     ng-show="volume_upload_form.volume.$invalid">

                    <small class="error"
                           ng-show="volume_upload_form.volume.$error.fileExtValidation">
                        File must be of type ".nii"
                    </small>
                </div>
            </div>

你可以看到

file-model="upload.fileModel"

将文件对象从文件输入字段链接到角度控制器。 这是正确的请求负载。

------WebKitFormBoundaryR1AXAwLepSUKJB3i
Content-Disposition: form-data; name="file"; filename="test.nii.gz"
Content-Type: application/x-gzip


------WebKitFormBoundaryR1AXAwLepSUKJB3i--
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot 文件上传错误请求 400 的相关文章

  • Spring Boot 删除 Whitelabel 错误页面

    我正在尝试删除白色标签错误页面 所以我所做的是为 error 创建了一个控制器映射 RestController public class IndexController RequestMapping value error public
  • 使用 Spring Boot 进行 Kafka 流

    我想在我的 Spring Boot 项目中使用 Kafka Streams 实时处理 所以我需要 Kafka Streams 配置或者我想使用 KStreams 或 KTable 但我在互联网上找不到示例 我做了生产者和消费者 现在我想实时
  • ios 无法将图片上传到服务器

    您好 我正在尝试将图像从我的 IOS 设备上传到服务器 这是我上传图像的代码 IBAction btnUpload id sender if self imageViewGallery image nil UIAlertView Error
  • 在 Spring Boot 中重新加载/刷新缓存

    我正在使用 Spring Boot 对于缓存 我使用 Ehcache 到目前为止一切正常 但现在我必须重新加载 刷新 那么我该如何执行此操作 以便我的应用程序不会出现任何停机时间 我在Spring Ehcache中尝试了很多方法 但它不起作
  • 在应用程序启动时将实例注册为“单例”bean

    我正在使用 Spring Boot 我正在尝试构建一个实例ServiceImpl时要解决Service是必须的 目前我将实现注释为 Component但这并没有给我机会构建我想要的实例 The ServiceImpl应使用包含磁盘上文件路径
  • 如何在 Spring Integration 流程中访问 Flux?

    我尝试访问 Spring Integration 中的 Flux 对象 而不将流声明拆分为两个函数 我想知道如何执行以下操作 Bean public IntegrationFlow mainFlow return IntegrationFl
  • Spring Data Redis 覆盖默认序列化器

    我正在尝试创建一个RedisTemplatebean 将具有更新的值序列化器来序列化对象JSONredis 中的格式 Configuration class RedisConfig Bean name redisTemplate Prima
  • Spring Data Jpa OneToMany 同时保存子实体和父实体?

    这是我的父实体 注意 为了简洁起见 删除了 getter setter lombok 注释 Entity public class Board Id GeneratedValue strategy GenerationType IDENTI
  • 使用 Spring Security 时,SecurityContext 是否在请求之间共享?

    在使用 Spring Boot 编写的 Rest API 上使用基于无状态令牌的身份验证时 我看到一些奇怪的行为 客户端在每个请求中包含一个 JWT 令牌 并且我编写的扩展 GenericFilterBean 的自定义过滤器使用以下命令将基
  • Spring SpEL - 用于创建字符串和自定义对象映射的表达式语言

    我在用着春季启动从属性文件中读取以下内容的示例 sub region data AF subRegionCd 34 subRegionName Southern Asia subRegionDesc status A 我在下面使用过 但不起
  • WCF 4.0 REST 上传 MS-Excel 文件

    我正在尝试通过 WCF REST 服务上传 MS Excel 文件 我使用了下面帖子中给出的解决方案 RESTful WCF服务图片上传问题 https stackoverflow com questions 664712 restful
  • 作出选择之后、提交上传之前的 asp.net FileUpload 事件

    我想显示通过 FileUpload 控件的浏览按钮选择的文件的大小 理想情况下 该值在用户选择文件后但在单击 上传文件 按钮之前立即显示 我有一个网络表格 按钮看起来像这样
  • 如何在不同的班级中启动和停止计时器?

    我想测量从传入 HTTP 请求开始到应用程序到达某个点的时间 这两个时间点都位于不同的类中 我将如何启动和停止这些不同类别的计时器 我没有看到使用 MeterRegistry 中的 命名 计时器的方法 我该怎么办呢 您可以使用 AOP 如下
  • 如何实现可变虚拟成员函数

    所以我有这个功能 virtual void CallRemoteFunction const char pServerGameObjectId const char pFunctionName OVariant arg1 OVariant
  • 在测试中替换 OAuth2 WebClient

    我有一个小型 Spring Boot 2 2 批次 用于写入 OAuth2 REST API 我已经能够配置WebClient下列的https medium com asce4s oauth2 with spring webclient 7
  • Jackson 的 ObjectMapper 和 SQL 中的 RowMapper

    我们正在使用对象映射器 当将 ObjectMapper 与 RowMapper 一起使用时 是否应该在每个 mapRow 内部 如下所示 声明它 还是在 mapRow 外部声明为类公共成员 我认为根据本文 它应该作为公共类成员在外部 我应该
  • 移动设备:缺少操作

    我正在尝试执行该操作的 POST 但是 当我发出请求时 我收到代码 400 表示操作值丢失 my code function mobileAPIPOST var response UrlFetchApp fetch https www go
  • 在 HTTP PATCH 请求中包含数据的正确方法

    当我组合 HTTP PATCH 请求时 可以选择哪些选项来包含 URL 参数之外的数据 以下任何一项都有效吗 最常见的选择是什么 多部分 表单数据 应用程序 x www form urlencoded Raw JSON 还有其他的吗 HTT
  • 用 @DataJpaTest 注释的测试不是用 @Autowired 注释的自动装配字段

    我有一个 Spring Boot 应用程序 其中包含 Spring Data Jpa 存储库 我需要围绕这个存储库运行单元 或组件 测试 我对 Spring Data Jpa 没有太多经验 这是我的测试 这很简单 我无法让它通过 impor
  • Spring Boot MSSQL Kerberos 身份验证

    目前在我的春季靴子中application properties文件中 我指定以下行来连接到 MSSql 服务器 spring datasource url jdbc sqlserver localhost databaseName spr

随机推荐