使用改造在服务器上上传文件

时间:2013-12-10 22:53:14

标签: android file-upload multipart 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

我认为服务器的方法可能写错了,但我不确定。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:9)

Uri#getPath返回Uri的路径组件,而不是文件系统上的路径。

您需要向内容提供商查询实际图片。这样的事情:https://stackoverflow.com/a/20186918/132047