Upload a image through an HTTP form, via MultipartEntity (how to change media type from application to image)

别等时光非礼了梦想. 提交于 2019-12-04 22:12:56

Apply this


private String doFileUpload() {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    myimage.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] imagebyte = stream.toByteArray();

    System.out.println(imagebyte + "...................................");


    String urlString = "your url";

    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(urlString);
        MultipartEntity reqEntity = new MultipartEntity();

        Calendar cal = Calendar.getInstance();
        String image_name = cal.getTime().toString();
        image_name = image_name.replace(" ", "");
        image_name = image_name.replace(":", "");
        image_name = image_name.substring(3, 14);

        //userfile
        reqEntity.addPart("featured_img", new ByteArrayBody(imagebyte,
                "Chasin_" + image_name + "_image.jpg"));




        reqEntity.addPart("title", new StringBody(title_var));
        reqEntity.addPart("description",new StringBody(description_var));
        reqEntity.addPart("category",new StringBody(cat_var));
        reqEntity.addPart("tags",new StringBody(tag));

        reqEntity.addPart("userid",new StringBody(Constants.USER_ID));


        post.setEntity(reqEntity);

        HttpResponse response = client.execute(post);
         HttpEntity resEntity = response.getEntity();
         response_str = EntityUtils.toString(resEntity);




    } catch (Exception ex) {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
    return response_str;
}

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());
}

I have changed Content-Type: application/octet-stream\r\n" to Content-Type: image/jpeg\r\n\r\n and it worked :)

Ashish
File imgFile = new File(Environment.getExternalStorageDirectory() + "/photo1/image2.jpg");

if (imgFile.exists()) {
    myimage = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    // im.setImageBitmap(myimage);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!