HttpMediaTypeNotSupportedException: 内容类型 'application/form-data' 不受支持 spring-boot
HttpMediaTypeNotSupportedException: Content type 'application/form-data' not supported spring-boot
在 Jhipster 上配置这些文件 application.yml、application-dev.yml、application-prod.yml 后:
spring:
application:
name: xxx
http:
multipart:
enabled: true
max-file-size: 200MB
file-size-threshold: 2MB
max-request-size: 215MB
我收到错误:
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:221)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:374)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:314)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:61)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:352)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1160)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:940)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
这是我的控制器的一个例子:
package com.xxx.xxx.web.rest;
import com.xxx.xxx.service.FileUploadDownloadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.MalformedURLException;
@RestController
@RequestMapping("/api")
public class FileUploadDownloadController {
private static final Logger logger = LoggerFactory.getLogger(FileUploadDownloadController.class);
private final FileUploadDownloadService service;
public FileUploadDownloadController(FileUploadDownloadService fileUploadDownloadService) {
this.service = fileUploadDownloadService;
}
@PostMapping(value = "/file/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public UploadFileResponse uploadFile(
@RequestParam("filename") String name,
@RequestParam("file") MultipartFile file) {
String fileName = service.uploadFile(file);
return new UploadFileResponse(fileName);
}
}
这是我要上传的 Angular 服务:
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {Headers, Http, RequestOptions} from '@angular/http';
@Injectable()
export class FileService {
private resourceUrl = 'api/file';
constructor(private http: Http) {
}
uploadSingleFile(file: File): Observable<any> {
const formData: FormData = new FormData();
formData.append('file', file);
const headers = new Headers();
headers.append('Content-Type', 'application/form-data');
const options = new RequestOptions({ headers });
console.log(formData);
return this.http.post(`${this.resourceUrl}/uploadFile`, formData, options );
}
}
我的spring-boot版本是1.5.4.RELEASE,
Java 版本 1.8.0_275,
节点版本 10.17,
JHipster 版本 4.6.0
我读了很多帖子,所有帖子都说了与我所做的相同的配置(启用了多部分支持),有没有我被误解或遗忘的地方?
感谢您的支持。
阅读一些 post 后,我发现没有必要指明内容类型如果您尝试发送 multipart/form-data,而我尝试发送的内容类型不正确,因为不不存在,所以我删除了那一行:
headers.append('Content-Type', 'application/form-data');
所以,就是这样。我希望这可以帮助某人。
此致。
在 Jhipster 上配置这些文件 application.yml、application-dev.yml、application-prod.yml 后:
spring:
application:
name: xxx
http:
multipart:
enabled: true
max-file-size: 200MB
file-size-threshold: 2MB
max-request-size: 215MB
我收到错误:
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:221)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:374)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:314)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:61)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:352)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1160)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:940)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
这是我的控制器的一个例子:
package com.xxx.xxx.web.rest;
import com.xxx.xxx.service.FileUploadDownloadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.MalformedURLException;
@RestController
@RequestMapping("/api")
public class FileUploadDownloadController {
private static final Logger logger = LoggerFactory.getLogger(FileUploadDownloadController.class);
private final FileUploadDownloadService service;
public FileUploadDownloadController(FileUploadDownloadService fileUploadDownloadService) {
this.service = fileUploadDownloadService;
}
@PostMapping(value = "/file/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public UploadFileResponse uploadFile(
@RequestParam("filename") String name,
@RequestParam("file") MultipartFile file) {
String fileName = service.uploadFile(file);
return new UploadFileResponse(fileName);
}
}
这是我要上传的 Angular 服务:
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {Headers, Http, RequestOptions} from '@angular/http';
@Injectable()
export class FileService {
private resourceUrl = 'api/file';
constructor(private http: Http) {
}
uploadSingleFile(file: File): Observable<any> {
const formData: FormData = new FormData();
formData.append('file', file);
const headers = new Headers();
headers.append('Content-Type', 'application/form-data');
const options = new RequestOptions({ headers });
console.log(formData);
return this.http.post(`${this.resourceUrl}/uploadFile`, formData, options );
}
}
我的spring-boot版本是1.5.4.RELEASE, Java 版本 1.8.0_275, 节点版本 10.17, JHipster 版本 4.6.0
我读了很多帖子,所有帖子都说了与我所做的相同的配置(启用了多部分支持),有没有我被误解或遗忘的地方?
感谢您的支持。
阅读一些 post 后,我发现没有必要指明内容类型如果您尝试发送 multipart/form-data,而我尝试发送的内容类型不正确,因为不不存在,所以我删除了那一行:
headers.append('Content-Type', 'application/form-data');
所以,就是这样。我希望这可以帮助某人。
此致。