Używam Spring Boot version = '1.4.0.RC1' z Spring Boot Stormpath 1.0.2.Spring Boot plik wieloczęściowy zawsze null
Próbuję użyć pliku wieloczęściowego, ale plik MultipartFile zawsze ma wartość null w kontrolerze.
Gdy używam @RequestPart ("plik") info: "status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'file' is not present"
Gdy używam @RequestPart (name = "file", wymagane = false), część jest zawsze zerowa.
Jednakże, jeśli dodaję do kontrolera argument HttpServletRequest, mogę pobrać część pliku bezpośrednio z żądania, więc wiem, że jest obecna.
Jest to kontroler, a w kodzie poniżej checkNotNull(part)
zawsze udaje i checkNotNull(imageFile)
zawsze kończy się niepowodzeniem:
@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();
}
Moja próba integracji wykorzystuje modernizacyjny:
@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);
}
Używam automatycznej konfiguracji więc moim jedynym zwyczajem konfiguracja klasy konfiguracji Ścieżka burzy:
@Configuration
public class SpringSecurityWebAppConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.apply(stormpath());
}
}
AKTUALIZACJA: To jest żądanie wychodzące. Nie jestem pewien, jak włączyć logowanie w samym tłumaczeniu wieloczęściowym.
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)
Jakieś pomysły na to, co się dzieje?
Czy możesz podać, jak wygląda ładunek żądania? Może nawet dodać rejestrowanie poziomu debugowania do 'MultipartResolver', którego używasz do sprawdzenia, czy interpretuje on składową wieloczęściową żądania? –
@ shawn-clark To jest wychodzące żądanie. Nie jestem pewien, jak włączyć logowanie w samym tłumaczeniu wieloczęściowym. Nagłówki żądań: Accept: application/json autoryzacji: Nośnik [uwierzytelniania znacznik] żądania dotyczącego: --56436527-d311-4d26-8e67-27fdf6f0edb8 Content-Disposition: postać transmisji danych; name = "plik" Content-Transfer-Encoding: binarny Content-Type: image/* Content-Length: 180999 [... binarny ...] --56436527-d311-4d26-8e67- 27fdf6f0edb8-- – JabariP
Podczas korzystania z wiosennego rozruchu 1.4 możesz użyć mojego rozwiązania i działa – rajadilipkolli