Android如何通过HttpURLConnection发送文件和参数

Android如何通过HttpURLConnection发送文件和参数,android,httpurlconnection,sendfile,Android,Httpurlconnection,Sendfile,我正在开发一个应用程序,这一个从sd卡发送图片,但现在我需要发送一些参数,我如何才能做到这一点 // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutp

我正在开发一个应用程序,这一个从sd卡发送图片,但现在我需要发送一些参数,我如何才能做到这一点

               // Open a HTTP  connection to  the URL
               conn = (HttpURLConnection) url.openConnection(); 
               conn.setDoInput(true); // Allow Inputs
               conn.setDoOutput(true); // Allow Outputs
               conn.setUseCaches(false); // Don't use a Cached Copy
               conn.setRequestMethod("POST");
               conn.setRequestProperty("Connection", "Keep-Alive");
               conn.setRequestProperty("ENCTYPE", "multipart/form-data");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
               conn.setRequestProperty("uploaded_file", fileName); 

               dos = new DataOutputStream(conn.getOutputStream());

               dos.writeBytes(twoHyphens + boundary + lineEnd);
               //dos.writeBytes (urlParameters);
               dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                                         + fileName + "\"" + lineEnd);

               dos.writeBytes(lineEnd);

非常感谢

您可以使用
MultiPartEntity
借助它,您可以上载多个文件和参数。可能会有帮助。

您可以通过多个部件传递文件和参数,如下所示:

public String reportCrime(String uploadFile, int userid, int crimetype,
        String crimedetails, String lat, String longi, String reporteddate) {
    String url;
    MultipartEntity entity;
    try {
        url = String.format(Constant.SERVER_URL
                + "push_notification/reportCrime.php");

        entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        //
        File file = new File(uploadFile);
        if (!file.equals("Image not Provided.")) {
            if (file.exists()) {

                Bitmap bmp = BitmapFactory.decodeFile(uploadFile);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bmp.compress(CompressFormat.JPEG, 70, bos);
                InputStream in = new ByteArrayInputStream(bos.toByteArray());
                ContentBody foto = new InputStreamBody(in, "image/jpeg", uploadFile);
                entity.addPart("image", foto);
            }
        } else {
            FormBodyPart image = new FormBodyPart("image", new StringBody(
                    ""));
            entity.addPart(image);
        }

        FormBodyPart userId = new FormBodyPart("userId", new StringBody(
                String.valueOf(userid)));
        entity.addPart(userId);

        FormBodyPart crimeType = new FormBodyPart("crimetype",
                new StringBody(String.valueOf(crimetype)));
        entity.addPart(crimeType);

        FormBodyPart crimeDetails = new FormBodyPart("crimedetail",
                new StringBody(crimedetails));
        entity.addPart(crimeDetails);

        FormBodyPart latittude = new FormBodyPart("latittude",
                new StringBody(lat));
        entity.addPart(latittude);

        FormBodyPart longitude = new FormBodyPart("longitude",
                new StringBody(longi));
        entity.addPart(longitude);

        FormBodyPart reportedDate = new FormBodyPart("reporteddatetime",
                new StringBody(reporteddate));
        entity.addPart(reportedDate);

    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        return "error";
    }

    HttpParams httpParams = new BasicHttpParams();

    HttpContext httpContext = new BasicHttpContext();
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
    HttpConnectionParams.setSoTimeout(httpParams, 10000);

    try {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(entity);
        client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent()));
            StringBuffer sb = new StringBuffer();
            String line = null;
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }

            result = sb.toString();
        } finally {
            if (in != null)
                in.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

您可以使用
MultiPartEntity