【问题标题】:Android how to send file and params by HttpURLConnectionAndroid如何通过HttpURLConnection发送文件和参数
【发布时间】:2014-06-25 19:25:54
【问题描述】:

我正在开发一个应用程序,这个是从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

标签: android httpurlconnection sendfile


【解决方案1】:

您可以使用MultiPartEntity 在它的帮助下上传多个文件和参数。 this 可能会有所帮助。

【讨论】:

    【解决方案2】:

    您可以像这样在多方中传递文件和参数:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多