【问题标题】:500 Internal server error when using multipart使用多部分时出现 500 内部服务器错误
【发布时间】:2016-02-26 09:02:08
【问题描述】:

在过去的几天里,我真的一直在纠结这个问题。我需要实现的是通过 Multipart 将图像发送到服务器。我试过这段代码,但在服务器上它说 请求不是多部分请求

这就是我尝试在我的应用上将图像发送到服务器的方式

MultipartEntity entity = new MultipartEntity();
entity.addPart(photosPath.getName(), new FileBody(photosPath));

这也是我的应用使用 RESTful 服务调用服务器的方式

@GET("/api/add/remark")
void addRemark(@Header("Cookies") HttpCookie cookie, @Query("tenantId") long tenantId, @Query("userId") long userId, @Query("comment") String commentJson, @Query("file") MultipartEntityBuilder multipartEntity, Callback<CreationResponse> callback);

在我的服务器端这是我接收的方式

@RequestMapping("add/remark")
@Secured({"ROLE_ADMIN","ROLE_SITE_USER","ROLE_FIELDUSER"})
@JsonInclude(Include.NON_NULL)
public @ResponseBody 
CreationResponse addRemarkController1(@RequestParam String comment,@RequestParam Integer userId,@RequestParam long tenantId,@RequestParam("file") MultipartFile file,HttpServletRequest request) {}

我不知道如何解决这个问题。请帮忙

编辑 1

MultipartEntity entity = new MultipartEntity();
ContentBody contentBody = new FileBody(photosPath,"imgae/jpeg");
entity.addPart("file", contentBody);

我试过了,还是不行

【问题讨论】:

    标签: android multipart multipartentity


    【解决方案1】:

    我认为,你应该使用@Multipart 注释。我就是这样做的

      @Multipart
    @POST("/events")
    CreateChatEventCallback sendChatImageSynchronously(@Header("Authorization") String token,
                                                       @Part("type") String type,
                                                       @Part("attachment") TypedFile image,
                                                       @Part("message_id") String messageId);
    

    This article 可能会有所帮助,

    【讨论】:

      最近更新 更多