org.springframework.web.client.ResourceAccessException:I / O错误:类路径资源[storage / emulated / 0 / DCIM / Camera / IMG_20190130_135103614.jpg]无法解析为URL,因为它不存在;嵌套的异常是java.io.FileNotFoundException:类路径资源[storage / emulated / 0 / DCIM / Camera / IMG_20190130_135103614.jpg]无法解析为URL,因为它不存在。
尝试从图库上载图像时出现此错误。 同一代码的工作,当图像被从绘制文件夹上传。
@Override
protected void onPreExecute() {
showLoadingProgressDialog();
Resource resource = new ClassPathResource(file.getPath());
formData = new LinkedMultiValueMap<String, Object>(); "jpg");
formData.add("description", "image");
formData.add("file", resource);
}
@Override
protected String doInBackground(Void... params) {
try {
// The URL for making the POST request
final String url = getString(R.string.base_uri) + "/***";
HttpHeaders requestHeaders = new HttpHeaders();
// Sending multipart/form-data
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
// Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(
formData, requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate(true);
// Make the network request, posting the message, expecting a String in response from the server
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
String.class);
// Return the response body to display to the user
return response.getBody();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
服务器端代码是
@RequestMapping(value = "/*", method = RequestMethod.POST, headers =
"Content-Type=multipart/form-data")
public @ResponseBody String
handleFormUpload(@RequestParam("description") String description,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
byte[] bytes = null;
try {
bytes = file.getBytes();
} catch (IOException e) {
System.out.println("error processing uploaded file");
}
return "file upload received! Name:[" + description + "] Size:["
+ bytes.length + "]";
} else {
return "file upload failed!";
}
}