使用multipart实体android在服务器上上传大文件

时间:2014-05-23 04:14:19

标签: php android

您好我在服务器上使用multipart实体上传文件,但它只在php服务器上传小文件 现在我想使用multipart实体上传大文件,那么我该怎么做呢?

- 我从sdcard获取大文件

public String uploadfile(String uploadFile, String crimedetails, String lat) 
{
    String url;
    MultipartEntity entity;
    try {
        url = String.format(WSConstants.SERVER_URL
                + WSConstants.URL_SET_POST);

        entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        //
        File file = new File(uploadFile);

        if (file.exists())
        {
            InputStream inputStream = null;
            ByteArrayOutputStream bos = null;
            try 
            {
                inputStream = new FileInputStream(file);
                bos = new ByteArrayOutputStream();
                //byte[] b = new byte[1 * 1024 * 1024];
                byte[] b = new byte[1024 * 8];
                int bytesRead = 0;

                while ((bytesRead = inputStream.read(b)) != -1)
                {
                    System.gc();
                    bos.write(b, 0, bytesRead);
                    bos.flush();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            InputStream in = new ByteArrayInputStream(bos.toByteArray());
            ContentBody foto = new InputStreamBody(in, "application/pdf",uploadFile);

            entity.addPart("uploadfile", foto);
        }

        else {
            FormBodyPart image = new FormBodyPart("uploadfile",
                    new StringBody(""));
            entity.addPart(image);
        }

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

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

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

    HttpParams httpParams = new BasicHttpParams();

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

    String result = null;
    try {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(entity);
        HttpClient 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;
}

1 个答案:

答案 0 :(得分:1)

*  # To upload the image from android mobile to server(php).Pass image path as a argument of uplaodImage() method and for backend use php script.That is in the last of Android code.*/





          public void uploadImage(String filePath)
            {
                try
                {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(Constants.url);
                    FileBody filebodyImage = new FileBody(new File(filePath));

                    StringBody title = new StringBody("Filename: " + filePath);

                    StringBody description = new StringBody(
                            "This is a description of the video");
                    MultipartEntity multipart=new MultipartEntity();
                    multipart.addPart("Image",filebodyImage);
                    multipart.addPart("title",title);
                    multipart.addPart("description", description);
                    httpPost.setEntity(multipart);
                    httpPost.setEntity(multipart);
                    System.out.println("Executing Request "+httpPost.getRequestLine());
                    HttpResponse httpResponse=httpClient.execute(httpPost);
                    HttpEntity httpEntity=httpResponse.getEntity();
                    System.out.println( httpResponse.getStatusLine( ) );
                    if (httpEntity != null) 
                    {
                      System.out.println( EntityUtils.toString( httpEntity ) );
                    } 
                    // end if
                    if (httpEntity != null) {
                        httpEntity.consumeContent( );
                      } // end if

                      httpClient.getConnectionManager( ).shutdown( );
                      System.out.println("Uplaod file Executed");
                }
                catch(ParseException e)
                {
                    e.printStackTrace();
                }
                catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }




 # Php Script
    file name=UplaodServer.php

 <?php
   $file_path =  basename( $_FILES['Image']['name']) ;
    if(move_uploaded_file($_FILES['Image']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
 ?>