[英]Uploading file on server using retrofit
在我的項目中,我必須將從Android設備拍攝的圖像發送到服務器站點,並將其存儲在磁盤上。 不幸的是,當我在設備網站上調用該方法時,我遇到了這個錯誤:
DEBUG/Retrofit(4429): java.lang.RuntimeException:
Unable to write multipart request.
at retrofit.mime.MultipartTypedOutput.buildPart(MultipartTypedOutput.java:86)
at retrofit.mime.MultipartTypedOutput.addPart(MultipartTypedOutput.java:49)
at retrofit.RequestBuilder.setArguments(RequestBuilder.java:211)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:264)
at retrofit.RestAdapter$RestHandler.access$500(RestAdapter.java:197)
at retrofit.RestAdapter$RestHandler$1.obtainResponse(RestAdapter.java:243)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:38)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at retrofit.Platform$Android$2$1.run(Platform.java:134)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.io.FileNotFoundException: /external/images/media/1270:
open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at retrofit.mime.TypedFile.writeTo(TypedFile.java:74)
at retrofit.mime.MultipartTypedOutput.buildPart(MultipartTypedOutput.java:83)
... 10 more
Caused by: libcore.io.ErrnoException:
open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:400)
... 13 more
這是方法的聲明
@Multipart
@POST("/monument/photo/upload")
void addMonumentPhoto(@Part("MonumentID") int monumentId,
@Part("name") String name,
@Part("subscript") String subscript,
@Part("photo") TypedFile photo,
Callback<Photo> callback);
......這就是我所說的
photo = new File(selectedImageUri.getPath());
typedFile = new TypedFile("application/octet-stream", photo);
MonumentsUtil.getApi().addMonumentPhoto(monument.getIdZabytek(),
"podpis",
"Main photo",
typedFile,
new Callback<Photo>() {
@Override
public void success(Photo aPhoto, Response response) {
}
@Override
public void failure(RetrofitError retrofitError) {
Log.e(TAG, retrofitError.getMessage());
}
});
在服務器的網站上我有這樣的方法:
@RequestMapping(value="/monument/photo/upload", method=RequestMethod.POST)
public @ResponseBody Photo requestMonumentPhotoAdd(
@RequestParam("MonumentID") int monumentId,
@RequestParam("name") String name ,
@RequestParam("subscript") String subscript,
@RequestParam("photo") org.springframework.web.multipart.MultipartFile file) {
Photo photo = new Photo();
photo.setIdZabytek(monumentId);
photo.setUri(URL+ "/images/" + name);
photo.setPodpis(subscript);
photo = monumentsRepo.addPhoto(photo);
String filePath = "D:\\Projekty\\Images\\" + monumentId + ":" + photo.getIdZjecia();
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(filePath)));
stream.write(bytes);
stream.close();
photo.setUri(filePath);
monumentsRepo.updatePhoto(photo);
return photo;
} catch (Exception e) {
return null;
}
} else {
return null;
}
}
程序到達故障塊,我不知道為什么。 任何人都可以指出錯誤並解釋什么是錯的嗎?
編輯
在我的代碼中糾正后,我有下一個錯誤,我無法修復:
java.net.ProtocolException: content-length promised 1431984 bytes, but received 0
com.squareup.okhttp.internal.http.RetryableOutputStream.close(RetryableOutputStream.java:52),
com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:629),
com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java: 346),
com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:295),
com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:489),
retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:90),
retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:48),
retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:287),
retrofit.RestAdapter$RestHandler.access$500(RestAdapter.java:197),
retrofit.RestAdapter$RestHandler$1.obtainResponse(RestAdapter.java:243),
retrofit.CallbackRunnable.run(CallbackRunnable.java:38),
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076),
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569),
retrofit.Platform$Android$2$1.run(Platform.java:134),
java.lang.Thread.run(Thread.java:856)
和HTTP響應
HTTP/1.1 400 Bad Request,
Connection: close,
Content-Length: 1068,
Content-Type: text/html;charset=utf-8,
Date: Wed, 11 Dec 2013 16:45:39 GMT,
OkHttp-Received-Millis: 1386780339873,
OkHttp-Response-Source: NETWORK 400,
OkHttp-Selected-Transport: http/1.1,
OkHttp-Sent-Millis: 1386780339799, Server: Apache-Coyote/1.1
我認為服務器的方法可能寫錯了,但我不確定。 有人可以幫忙嗎?
Uri#getPath
返回的路徑組件Uri
,而不是在文件系統中的路徑。
您需要向內容提供商查詢實際圖像。 這樣的事情: https : //stackoverflow.com/a/20186918/132047
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.