2014-05-23 86 views
1

嗨我上傳文件在服務器上使用多部分實體,但它只上傳php服務器上的小文件 現在我想上傳使用多部分實體的大文件,所以我怎麼做到這一點?上傳使用多部分實體的服務器上的大文件android

-I是從SD卡獲取大文件

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

檢查該SO帖子,希望這將有助於ü.... HTTP://stackoverflow.com/questions/21511981/upload-a -file到一個-URL/21513158#21513158 – ajitksharma

回答

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"; 
    } 
?> 
相關問題