मैं स्प्रिंग बूट स्टॉर्मपाथ 1.0.2 के साथ स्प्रिंग बूट संस्करण = '1.4.0.आरसी 1' का उपयोग कर रहा हूं।स्प्रिंग बूट मल्टीपार्टफाइल हमेशा शून्य
मैं मल्टीपार्ट फ़ाइल अपलोड का उपयोग करने की कोशिश कर रहा हूं लेकिन मल्टीपार्टफाइल हमेशा नियंत्रक में शून्य है।
जब मैं @RequestPart का उपयोग करें ("फाइल") जानकारी: "status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'file' is not present"
जब मैं @RequestPart (नाम = "फाइल", = आवश्यक false) का उपयोग, हिस्सा हमेशा रिक्त है।
हालांकि, अगर मैं नियंत्रक को एक HttpServletRequest तर्क जोड़ता हूं, तो मैं सीधे अनुरोध से फ़ाइल भाग प्राप्त कर सकता हूं, इसलिए मुझे पता है कि यह वास्तव में मौजूद है।
इस नियंत्रक है और checkNotNull(part)
नीचे कोड में हमेशा सफल होता है और checkNotNull(imageFile)
हमेशा विफल रहता है:
@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();
}
मेरे एकीकरण परीक्षण का उपयोग करता है पुराना वापस:
@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);
}
मैं तो मेरे ही कस्टम ऑटो विन्यास का उपयोग कर रहा कॉन्फ़िगर क्लास स्टॉर्मपाथ को कॉन्फ़िगर करता है:
@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)
क्या हो रहा है पर कोई विचार?
क्या आप अनुरोध कर सकते हैं कि अनुरोध पेलोड कैसा दिखता है? हो सकता है कि आप 'मल्टीपार्ट रीसोल्वर' को डीबग लेवल लॉगिंग भी जोड़ सकें जिसका उपयोग आप यह देखने के लिए कर रहे हैं कि यह अनुरोध के मल्टीपार्ट घटक का अर्थ है या नहीं? –
@ शॉन-क्लर्क यह आउटगोइंग अनुरोध है। मुझे यकीन नहीं है कि मल्टीपार्ट रिज़ॉल्वर में लॉगिंग को कैसे सक्षम किया जाए। अनुरोध हेडर: स्वीकार करें: आवेदन/json प्राधिकरण: बियरर [प्रमाणीकरण टोकन] अनुरोध शरीर: --56436527-d311-4d26-8e67-27fdf6f0edb8 सामग्री-विन्यास: फार्म डेटा; नाम = "फाइल" सामग्री-स्थानांतरण-एन्कोडिंग: द्विआधारी सामग्री प्रकार: image/* सामग्री-लंबाई: 180,999 [... द्विआधारी ...] --56436527-d311-4d26-8e67- 27fdf6f0edb8-- – JabariP
जैसा कि आप वसंत बूट 1.4 का उपयोग कर रहे हैं, आप मेरे समाधान का उपयोग कर सकते हैं और यह – rajadilipkolli