简体   繁体   English

如何实现MultiPart方法在Android中上传图片

[英]How to implement MultiPart approach to upload image in Android

I want to add Image Upload functionality in my app. 我想在我的应用程序中添加图像上传功能。 The thing is that I want to upload a big size image to the server. 问题是我想将大尺寸的图片上传到服务器。 Thats why I want to use Multipart approach for this scenario. 这就是为什么我要在这种情况下使用Multipart方法的原因。 As I am new to this concept I don't know how to implement this. 由于我是这个概念的新手,所以我不知道如何实现它。 Also I have some specific queries regarding the image upload. 我也有一些有关图像上传的特定查询。

I need to send a POST request to the server in which I have to send JSON data, for example :- 我需要向必须发送JSON数据的服务器发送POST请求,例如:

JSONObject jsonObject = new JSONObject();

        jsonObject.put("filename", "menu.jpg");
        jsonObject.put("filetype", "image/jpeg");
        jsonObject.put("user_id", "1");
        jsonObject.put("user_file", "");

Here, the user file is the actual file which I want to upload and the above parameters are the one which I need to send with this Image file to the server and they all are mandatory to upload the file. 在这里,用户文件是我要上传的实际文件,而上面的参数是我需要与此映像文件一起发送到服务器的参数,它们都是必须上传的文件。

Now my question is that how can I send the JSON using MultiPart approach and should I use Volley or AsyncTask to perform the overall steps. 现在我的问题是,我该如何使用MultiPart方法发送JSON,并且应该使用Volley或AsyncTask来执行整个步骤。 Can anyone help me, any help would be appreciable. 任何人都可以帮助我,任何帮助都是可观的。 Thanks 谢谢

you have to work like this 你必须像这样工作

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

for getting response 获得回应

httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));



String sResponse;
while ((sResponse = reader.readLine()) != null) 
 {
     s = s.append(sResponse);
 }

 if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
 {
     return s.toString();
 }else
 {
     return "{\"status\":\"false\",\"message\":\"Some error occurred\"}";
 }   
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

In place of image you can send anything, you would like to... if it creates some heck then you should divide the large file into small parts then try to sending.And Join this small part on the server. 您可以代替图像来发送任何内容,如果您想要...如果它产生了一些麻烦,则应将大文件分成小部分,然后尝试发送。然后将此小部分加入服务器。

You will no need to add the image in the json object. 您无需将图像添加到json对象中。 Please go through this link. 请通过此链接。 I think you will solve your problem. 我想您会解决您的问题的。 http://www.survivingwithandroid.com/2013/05/android-http-downlod-upload-multipart.html http://www.survivingwithandroid.com/2013/05/android-http-downlod-upload-multipart.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM