通过HTTP的形式上传的图像,通过MultipartEntity(如何从应用到图像改变媒体类型) [英] Upload a image through an HTTP form, via MultipartEntity (how to change media type from application to image)

查看:123
本文介绍了通过HTTP的形式上传的图像,通过MultipartEntity(如何从应用到图像改变媒体类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code

            HttpClient httpClient = new DefaultHttpClient();           
            HttpPost httpPost = new HttpPost("http://www.hugosys.in/www.nett-torg.no/api/rpcs/uploadfiles/?");
            File file = new File("/mnt/sdcard/xperia.png");
            FileBody fileBody = new FileBody(file);
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("post_id", new StringBody("906"));
            reqEntity.addPart("user_id", new StringBody("1"));
            reqEntity.addPart("files", fileBody);
            httpPost.setEntity(reqEntity);                                      
            response = httpClient.execute(httpPost);                 
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            response_string =convertStreamToString(is);



            ..........

方法来解析响应

    private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append((line + "\n"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

我米从服务器获取的成功,但图像没有在服务器接收...西隧即时通讯做错了

I m getting success from the server, but image is not received on server ... wht i m doing wrong

推荐答案

大家好,我想我得上班这些事

Guys i think i have to work on these things

public void connectForMultipart() throws Exception {
    con = (HttpURLConnection) ( new URL(url)).openConnection();
    con.setRequestMethod("POST");
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    con.connect();
    os = con.getOutputStream();
}

public void addFormPart(String paramName, String value) throws Exception {
    writeParamData(paramName, value);
}

public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
    os.write( (delimiter + boundary + "\r\n").getBytes());
    os.write( ("Content-Disposition: form-data; name=\"" + paramName +  "\"; filename=\"" + fileName + "\"\r\n"  ).getBytes());
    os.write( ("Content-Type: application/octet-stream\r\n"  ).getBytes());
    os.write( ("Content-Transfer-Encoding: binary\r\n"  ).getBytes());
    os.write("\r\n".getBytes());

    os.write(data);

    os.write("\r\n".getBytes());
}   
public void finishMultipart() throws Exception {
    os.write( (delimiter + boundary + delimiter + "\r\n").getBytes());
}

我已经改变了内容类型:应用程序/八位字节流\ r \ N内容类型:图像/ JPEG \ r \ ñ\ r \ñ和它的工作:)

这篇关于通过HTTP的形式上传的图像,通过MultipartEntity(如何从应用到图像改变媒体类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆