使用HttpPost MultiPartEntityBuilder上传照片

 报告大王报报报报告大王 发布于 2023-02-13 19:38

我正在尝试将拍摄的照片上传到服务器.这是我做的:

public JSONObject makePostFileRequest(String url, String photoFile) {
    try {
        // photoFile = /path/tofile/pic.jpg
        DefaultHttpClient httpClient = GlobalData.httpClient;
        HttpPost httpPost = new HttpPost(url);

        File file = new File(photoFile);
        FileBody fileBody = new FileBody(file); // here is line 221

        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("PhotoMessage", fileBody);

        httpPost.setEntity(multipartEntity.build());

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

我收到此错误:

11-29 13:12:14.924:E/AndroidRuntime(15781):引起:java.lang.NoClassDefFoundError:org.apache.http.entity.ContentType 11-29 13:12:14.924:E/AndroidRuntime(15781):在org.apache.http.entity.mime.content.FileBody.(FileBody.java:89)11-29 13:12:14.924:E/AndroidRuntime(15781):at com.petcial.petopen.custom.JSONParser.makePostFileRequest (JSONParser.java:221)

我究竟做错了什么?


更新

InputStream inputStream;
inputStream = new FileInputStream(new File(photoFile));
byte[] data;
data = IOUtils.toByteArray(inputStream);

httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                        System.getProperty("http.agent"));
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "Pic.jpg");

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("PhotoMessage", inputStreamBody);

httpPost.setEntity(multipartEntity.build());

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

这是错误:

11-29 14:00:33.364:E/AndroidRuntime(19478):引起:java.lang.NoClassDefFoundError:org.apache.http.util.Args 11-29 14:00:33.364:E/AndroidRuntime(19478):在org.apache.http.entity.mime.content.AbstractContentBody.(AbstractContentBody.java:48)11-29 14:00:33.364:E/AndroidRuntime(19478):at org.apache.http.entity.mime.content .InputStreamBody.(InputStreamBody.java:69)11-29 14:00:33.364:E/AndroidRuntime(19478):at org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:62)11- 29 14:00:33.364:E/AndroidRuntime(19478):at com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:233)

这些库解决了我的问题:

2 个回答
  • 在此输入图像描述 在Order and Export选项卡中检查该jar并运行.

    2023-02-13 19:39 回答
  • 有我的工作解决方案,使用apache http库发送带有post的图像(这里非常重要的是边界添加它在我的连接中没有它将无法工作):

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(StaticData.AMBAJE_SERVER_URL + StaticData.AMBAJE_ADD_AMBAJ_TO_GROUP);
    
    String boundary = "-------------" + System.currentTimeMillis();
    
    httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
    
    ByteArrayBody bab = new ByteArrayBody(imageBytes, "pic.png");
    StringBody sbOwner = new StringBody(StaticData.loggedUserId, ContentType.TEXT_PLAIN);
    StringBody sbGroup = new StringBody("group", ContentType.TEXT_PLAIN);
    
    HttpEntity entity = MultipartEntityBuilder.create()
                        .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                        .setBoundary(boundary)
                        .addPart("group", sbGroup)
                        .addPart("owner", sbOwner)
                        .addPart("image", bab)
                        .build();
    
    httpPost.setEntity(entity);
    
    try {
           HttpResponse response = httpclient.execute(httpPost);
           ...then reading response
    

    2023-02-13 19:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有