android - 改造文件上传,对象在服务器端为空

标签 android file-upload multipart retrofit

我想将照片从本地 android 画廊发送到服务器 http Tomcat。对于我正在使用改造的通信。我已经建立了设备和服务器之间的连接,并且程序进入了服务器功能,但是参数中的所有对象都是空的。

这是客户端的设备函数声明:

@Multipart
@POST("/monument/photo/upload")
void addMonumentPhoto(@Part("MonumentID") Integer monumentId,
                      @Part("name") String name,
                      @Part("subscript") String subscript,
                      @Part("photo") TypedFile photo,
                      Callback<Photo> callback);

...我就是这样调用它的:

photo = _resizePhoto(new File(monument.getUriZdjecie()));
typedFile = new TypedFile("multipart/mixed", photo);
//long bytes = photo.length();

  if (photo.exists()) {
      MonumentsUtil.getApi().addMonumentPhoto(monument.getIdZabytek(),
          "podpis",
          "Main photo",
           typedFile,
           new Callback<Photo>() {
           @Override
           public void success(Photo aPhoto, Response response) {

                  monument.setUriZdjecie(aPhoto.getUri());

                  MonumentsUtil.getApi().addMonument(monument.getNazwa(),
                                            monument.getOpis(),
                                            monument.getDataPowstania(),
                                            monument.getWojewodztwo(),
                                            monument.getUriZdjecie(),
                                            monument.getMiejscowosc(),
                                            monument.getKodPocztowy(),
                                            monument.getUlica(),
                                            monument.getNrDomu(),
                                            monument.getNrLokalu(),
                                            monument.getKategoria(),
                                            monument.getLatitude(),
                                            monument.getLongitude(),
                                            new MonumentsCallback());
           }
           @Override
           public void failure(RetrofitError retrofitError) {
                 Log.e(TAG, retrofitError.getMessage());
           }
     });
}

和服务器的方法:

@RequestMapping(value = "/monument/photo/upload")
public
@ResponseBody
Photo requestMonumentPhotoAdd(@RequestParam(value = "MonumentID", required = false) Integer monumentId,
                              @RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "subscript", required = false) String subscript,
                              @RequestParam(value = "photo", required = false) MultipartFile file,
                              HttpServletRequest request) {

    Photo photo = new Photo();
    if (monumentId != null)
        photo.setIdZabytek(monumentId);
    photo.setUri(URL + "/images/" + name);
    photo.setPodpis(subscript);
    photo = monumentsRepo.addPhoto(photo);
    String filePath = "D:\\Projects\\Images\\" + monumentId + "_" + photo.getIdZjecia();

    if (file != null) {
        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;
        }
    }
    else {
        return null;
    }
}

谁能帮我解释为什么进入服务器方法后所有对象都是空的? 也许方法被错误地写入或者 TypedFile 的 mime 字段被错误地选择但我读到“multipart/mixed”mime 类型适用于消息中包含各种类型对象的消息。我不知道,所以任何建议都会有所帮助。

最佳答案

尝试在创建 TypedFile 对象时使用“image/*”作为您的 mime 类型。对于那个“部分”,它属于那个特定类型。 “混合”可能是作为一个整体提交,而不是文件的单个部分。

typedFile = new TypedFile("图像/*", 照片);

关于android - 改造文件上传,对象在服务器端为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590873/

相关文章:

javascript - 使用 XMLHttpRequest 上传大文件时的进度条

file-upload - Laravel错误的公共(public)路径文件上传

java - 接受列表作为使用多部分内容类型的 Jersey Web 服务的参数

android - 能否将 VectorDrawable pathData 转换为 Path 对象

android - JSON - 与 Android 应用程序一起使用的单个文件

android - 使用 Google API 的 Firebase 主题消息接收状态

android - 如何为 volley MultipartEntityBuilder 添加多个 Mime 类型

android - 禁用屏幕超时 Android 手机/平板电脑

c# - FileUpload 控件在回发时丢失其内容

go - 分段文件上传 : How to Handle FileHeaders