【问题标题】:how to send empty value for MultipartFile如何为 MultipartFile 发送空值
【发布时间】:2014-11-28 12:35:09
【问题描述】:

我正在使用 angularJs 和 spring 4.0,

我的控制器代码:

@RequestMapping(value = "/endpoint/add", method = RequestMethod.POST)
public @ResponseBody GenericFormResponse execute(
    WebRequest wreq,
    @RequestParam("epName") String epName,
    @RequestParam("ipAddr") String ipAddr,
    @RequestParam("useDefault") String useDefault,
    @RequestParam("certFile") MultipartFile certFile) throws Exception {
    .....................
}

我的 js(angualrJs) 代码:

var formData = new FormData();
formData.append("epName", $scope.epName);
formData.append("ipAddr", $scope.ipAddr);
formData.append("useDefault",$scope.defaultCert);
if(!$scope.defaultCert){
    formData.append("certFile", upFile);
}
$http({
    method: "POST",
    url: "./service/endpoint/add",
    data: formData,
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined }
}).success(svcSuccessHandler)
  .error(svcErrorHandler);

我的问题是$scope.defaultCert=false POST 请求工作正常,$scope.defaultCert = true 我收到 Bad request(400)

我也试过下面的东西,

if(!$scope.defaultCert){
    formData.append("certFile", upFile);
}else{
    formData.append("certFile", null);
}

我如何发送空的MultipartFile。 谢谢。

【问题讨论】:

    标签: java javascript jquery angularjs spring-mvc


    【解决方案1】:

    我在控制器中创建了两个服务,并为两者创建了两个 url

    @RequestMapping(value = "/endpoint/add-with-cert", method = RequestMethod.POST)
    public @ResponseBody GenericFormResponse excecuteWithCert(
            WebRequest wreq,
            @RequestParam("epName") String epName,
            @RequestParam("ipAddr") String ipAddr,
            @RequestParam("useDefault") boolean useDefault,
            @RequestParam("certFile") MultipartFile certFile) throws Exception {
        LOGGER.debug("received request for endpoint creation with certificate");
        GenericFormResponse response = new GenericFormResponse();
        SessionManager sessionMgr = new SessionManager(wreq);
        if(!sessionMgr.isLoggedIn()) {
            response.setSuccess(false);
            response.setGlobalErrorCode("not_logged_in");
            return response;
        }
        ...............
    }
    
    @RequestMapping(value = "/endpoint/add", method = RequestMethod.POST)
    public @ResponseBody GenericFormResponse excecuteWithOutCert(
            WebRequest wreq,
            @RequestParam("epName") String epName,
            @RequestParam("ipAddr") String ipAddr,
            @RequestParam("useDefault") boolean useDefault) throws Exception {
        LOGGER.debug("received request for endpoint creation  without certificate");
        ...............
    }
    

    在js文件中:

    var url = "./service/endpoint/add";
    if(!$scope.defaultCert){
        formData.append("certFile", upFile);
        url = "./service/endpoint/add-with-cert";
    }       
    $http({
    method: "POST",
        url: url,
        data: formData,
        transformRequest: angular.identity, headers: {'Content-Type': undefined }
    }).success(svcSuccessHandler)
    .error(svcErrorHandler);
    

    我不知道这种方法是否正确,但它满足了我的要求。按我的预期工作。请提出最佳答案。

    【讨论】:

      【解决方案2】:

      您需要正确配置才能在 Spring 4 中启用分段文件上传。

      将以下行添加到应用程序初始化程序类:

      dispatcher.setMultipartConfig(
              new MultipartConfigElement("/tmp", 25 * 1024 * 1024, 125 * 1024 * 1024, 1 * 1024 * 1024)
      );
      

      这里的dispatcher是ServletRegistration.Dynamic的一个实例

      更多详情请参考this答案。

      【讨论】:

      • 如果$scope.defaultCert=false我可以上传文件,一切正常。
      猜你喜欢
      • 1970-01-01
      • 2019-08-03
      • 2020-05-19
      • 2017-11-27
      • 2016-12-24
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多