当前位置: 首页 > 知识库问答 >
问题:

修改多部分上载图像失败

刘野
2023-03-14

我正在尝试上传图像与修改库。我就是这样上传的:

请求代码:

@Multipart
@POST("/customerapp/{context}/{token}/passenger/passport/add/{passengerId}")
@Headers({
        "Accept: application/xml",
        "Accept-Encoding: gzip"
})
void UploadImage(
        @Path("context") String context,
        @Path("token") String token,
        @Path("passengerId") String passengerId,
        @Query("fileType") String fileType,
        @Query("imageCategory") int imageCategory,
        @Part("imageContent") TypedFile file,
        Callback<VJSuccessResponse> callback
);



public static final String BASE_URL = 
    "http://webservicetest.abc.com/extranetServices/1.1";

RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                Log.e("Retrofit Request Body", request.toString());
            }
        };

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(BackendConstants.BASE_URL)
                .setClient(new OkClient(new OkHttpClient()))
                .setConverter(new SimpleXMLConverter())
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setRequestInterceptor(requestInterceptor)
                .build();

        REST_CLIENT = restAdapter.create(BackendAPI.class);

        REST_CLIENT.UploadImage(
                BackendConstants.CONTEXT,
                StateObject.sSPManager.getStoredPersonalDetails().getToken(),
                passengerId,
                new File(filePath),
                imageCategory,
                new TypedFile("image/jpeg", typeFile), new Callback<VJSuccessResponse>() {
                    @Override
                    public void success(VJSuccessResponse getCallResponse, Response response) {

                    }

                    @Override
                    public void failure(RetrofitError error) {


                        Log.d(TAG, error.toString());
                    }
                })

答复:

HTTP POST http://webservicetest.abc.com/extranetServices/1.1/customerapp/customerapp/cba75eb0d5d64e16b37cca477d68d836/passenger/passport/add/56672?fileType=jpg&imageCategory=1
Accept: application/xml
Accept-Encoding: gzip
Content-Type: multipart/form-data; boundary=fb1f78df-d674-4e54-9b41-32a386ca4222
Content-Length: 6038770
Content-Disposition: form-data; name="imageContent"; filename="userdp.jpg"
Content-Type: image/jpeg
Content-Length: 6038513

Content-Transfer-Encoding: binary
    ������JFIF����������������C������C�����,"��������������������������
    �����������}��!1AQa"q2���#B��R��$3br�
(That goes long String of garbage...)


<--- HTTP 200 http://webservicetest.abc.com/extranetServices/1.1/customerapp/customerapp/cba75eb0d5d64e16b37cca477d68d836/passenger/passport/add/56672?fileType=jpg&imageCategory=1 (109092ms)
Date: Thu, 05 Feb 2015 14:52:28 GMTServer: GlassFish Server Open Source Edition 3.1.2.2
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Sun Microsystems Inc./1.6)
Content-Encoding: gzip
Content-Type: application/xml
Content-Length: 108
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1423148584220
OkHttp-Received-Millis: 1423148693098
��������������������Q(K-*��ϳU2�3PRH�K�O��K�U
    qӵPR(.I�KI���K�U�L-V���)�
    J-.��+N��).MNN-.�+)*M�ч�l�������u��g������
<--- END HTTP (108-byte body)
retrofit.RetrofitError: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT �������������������Q...@3:38 in java.io.InputStreamReader@26e1231a)

如果我是通过客户端浏览器(如postman或DHC)发布的,则请求与上面相同,并且我得到一个成功的XML响应。

请看我在邮递员客户端上尝试的截图。它是成功的。

共有1个答案

经正祥
2023-03-14

我也有类似的问题,经过几个小时的尝试,我终于建立了映像上传功能到远程服务器。
要上传映像,您需要正确地创建API,也需要正确地传递映像。在“改装客户端”中,您需要按照以下步骤设置映像:

String photoName = "20150219_222813.jpg";
File photo = new File(photoName );
TypedFile typedImage = new TypedFile("application/octet-stream", photo);

RetrofitClient.uploadImage(typedImage, new retrofit.Callback<Photo>() {

    @Override
    public void success(Photo photo, Response response) {
        Log.d("SUCCESS ", "SUCCESS RETURN " + response);
    }

    @Override
    public void failure(RetrofitError error) {

    }
});

API设置:

@Multipart
@POST("/")
void uploadImage(@Part("file") TypedFile file, Callback<Photo> callback);

处理映像的远程服务器端PHP代码:

$pic = 'uploaded_images/' . $imagename . '.jpg';
if (!move_uploaded_file($_FILES['file']['tmp_name'], $pic)) {
   echo "posted";
}
 类似资料:
  • 我要哭了,因为我尝试了很多解决方案和话题,但都不起作用。 null 您需要发送一个多部分请求(enctype=“multipart/form-data”)。 所以,有人能请帮助我,为什么我有HTTP 400与上面的错误消息?我可以在代码中更改什么以使此调用工作?谢谢你

  • 我正在学习“将图像上传到服务器”,但当我尝试上传多个图像时,总是无法报告(JSON错误,来自iOS设备上的Xcode警报),似乎错误在PHP端?但我是新来的,所以任何人都可以帮我找到解决办法。Xcode: > httpBody函数 func createBodyWithParameters(参数:[字符串:任意]?,边界:字符串)- } http请求函数 } 多图像上载函数func uploadP

  • 我有一个问题上传文件到服务器(春季启动)从Android-改版。 这是我在Spring Boot中的代码。 这是在Android中 这是Spring Boot中的错误消息 但是,当我用邮递员的时候,它做得很好。 请告诉我问题是什么,如何解决

  • 我有以下错误: 我为http请求设置了服务器,仍然得到错误:

  • 位卡在这个上,需要上传图像和json一起使用多部分形式...不知道如何发送内容类型标头或上传图像...认为我需要转换为blob...目前我只是发送我从文件输入字段中获得的数据。 任何建议都很好谢谢

  • http://letitripple.org/htmlform.html 这是此html表单创建的请求。(我从chrome开发者工具中获得) ------WebKitFormBoundaryPug6XAUALAUPBR86内容-配置:表单-数据;name=“wp-user-avatars”;filename=“desert.jpg”内容-类型:image/jpeg