스프링 부트 스톰 패스 1.0.2와 함께 스프링 부트 버전 = '1.4.0.RC1'을 사용하고 있습니다.스프링 부트 멀티 파트 파일은 항상 null
멀티 파트 파일 업로드를 사용하려하지만 MultipartFile은 컨트롤러에서 항상 null입니다.
나는 ("파일")는 정보 @RequestPart를 사용하는 경우 : "status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'file' is not present"
나는 (= 거짓 필요한 이름 = "파일") @RequestPart를 사용하는 경우, 부품이 항상 null의
.
그러나 컨트롤러에 HttpServletRequest 인수를 추가하면 파일 부분을 요청에서 직접 가져올 수 있으므로 실제로 존재한다는 것을 알 수 있습니다.
이것은 컨트롤러 및 checkNotNull(part)
아래의 코드는 항상 성공하고 checkNotNull(imageFile)
는 항상 실패
@Multipart
@POST("users/{username}/profilePhoto")
Call<Void> uploadProfilePhoto(@Path("username") String username,
@Part("file") RequestBody profilePhoto);
...
@Test
public void saveProfilePhoto_shouldSavePhoto() throws IOException {
// Given
String usernamme = usernames[0];
Resource testImageResource = context.getResource("classpath:images/test_image.jpg");
File imageFile = testImageResource.getFile();
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("image/*"), imageFile);
// When
Response<Void> response = getTestApi().uploadProfilePhoto(usernamme, body).execute();
// Then
assertThat(response.code()).isEqualTo(201);
}
내가 자동 구성을 내 유일한 정의 있도록 사용하고 있습니다 :
@PostMapping("{username}/profilePhoto")
public ResponseEntity<?> saveProfilePhoto(@PathVariable("username") String username,
@RequestPart(name = "file", required = false) MultipartFile imageFile,
HttpServletRequest request) {
try {
Part part = request.getPart("file");
checkNotNull(part);
checkNotNull(imageFile);
} catch (IOException | ServletException ex) {
throw InternalServerErrorException.create();
}
// Transfer the multipart file to a temp file
File tmpFile;
try {
tmpFile = File.createTempFile(TMP_FILE_PREFIX, null);
imageFile.transferTo(tmpFile);
} catch (IOException ex) {
log.error("Failed to create temp file", ex);
throw InternalServerErrorException.create();
}
// Execute the use case
updateUserProfilePhoto.execute(username, tmpFile);
// Delete the temp file
FileUtils.deleteQuietly(tmpFile);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
내 통합 테스트는 개조가 사용 구성 클래스는 스톰 패스를 구성합니다.
@Configuration
public class SpringSecurityWebAppConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.apply(stormpath());
}
}
업데이트 : 발신 요청입니다. 멀티 파트 분석기 자체에서 로깅을 활성화하는 방법을 잘 모르겠습니다.
2016-08-18 14:44:14.714 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 : --> POST http://localhost:8080/users/user1/profilePhoto http/1.1
2016-08-18 14:44:14.714 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 : Content-Type: multipart/form-data; boundary=fe23ef21-3413-404c-a260-791c6921b2c6
2016-08-18 14:44:14.715 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 : Content-Length: 181212
2016-08-18 14:44:14.715 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 : Accept: application/json
2016-08-18 14:44:14.715 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 : Authorization: Bearer [token]
2016-08-18 14:44:14.715 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 :
2016-08-18 14:44:14.735 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 : --fe23ef21-3413-404c-a260-791c6921b2c6
Content-Disposition: form-data; name="file"
Content-Transfer-Encoding: binary
Content-Type: image/*
Content-Length: 180999
file data
--fe23ef21-3413-404c-a260-791c6921b2c6--
2016-08-18 14:44:14.762 DEBUG 13088 --- [ main] c.t.server.web.testutil.TestConfig$1 : --> END POST (181212-byte body)
무슨 일이 일어나고 있는지 아이디어가 있습니까?
요청 페이로드의 모양을 포함 할 수 있습니까? 디버그 레벨 로깅을 요청의 multipart 구성 요소를 해석하는지 확인하기 위해 사용중인'MultipartResolver'에 추가 할 수 있습니까? –
@ shawn-clark 이것은 나가는 요청입니다. 멀티 파트 분석기 자체에서 로깅을 활성화하는 방법을 잘 모르겠습니다. 요청 헤더 : 수락 : 애플리케이션/JSON 권한 : 베어러 [인증 토큰] 요청 본문 : --56436527-d311-4d26-8e67-27fdf6f0edb8 내용 - 처리 : 폼 데이터; = "파일" 콘텐츠 전송 인코딩 이름 : 진 내용 - 형식 : 이미지/* 콘텐츠 길이 : 180,999 [... 진 ...] --56436527-d311-4d26-8e67- 27fdf6f0edb8-- – JabariP
스프링 부트 1.4를 사용하고 있기 때문에 내 솔루션을 사용할 수 있습니다. – rajadilipkolli