2014-04-29 2 views
0

나는이 AsyncTask 클래스를 POST 데이터 배열을 문제없이 PHP 서버에 보냈습니다. 이제는 동일한 스크립트에 파일을 전송할 수 있도록 확장하고 싶습니다 (이미 PHP 파일에서 핸들링을 받았습니다). 내 말은 한 번에 DATA + FILE을 게시하는 것입니다. multipart 엔티티 또는 HTML 액션에서 PHP 스크립트에 이르기까지.안드로이드는 PHP 서버에 멀티 파트 HTML 폼을 게시합니다

다른 것들과 함께 파일을 업로드 할 수 있도록이 클래스에 무엇을 추가해야합니까?

public class UpdateSnakeGameStatusTask extends AsyncTask<String, Integer, HttpResponse> { 
    private Context mContext; 
    private ArrayList<NameValuePair> mPairs; 

    /** 
    * @param context The context that uses this AsyncTask thread 
    * @param postPairs <b>NameValuePair</b> which contains name and post data 
    */ 
    public UpdateSnakeGameStatusTask(Context context, ArrayList<NameValuePair> postPairs) { 
     mContext = context; 
     mPairs = new ArrayList<NameValuePair>(postPairs); 
    } 

    @Override 
    protected HttpResponse doInBackground(String... params) { 
     HttpResponse response = null; 
     HttpPost httppost = new HttpPost(params[0]); //this is the URL 

     try { 
      httppost.setEntity(new UrlEncodedFormEntity(mPairs)); 
      HttpClient client = new DefaultHttpClient(); 
      response = client.execute(httppost); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return response; 
    } 
} 
+0

가능한 중복 [HTTP를 사용하여 서버에 모바일에서 안드로이드에서 파일을 전송하는 방법?] (http://stackoverflow.com/questions/4126625/how-to-send하는 데 도움이 -a-file-in-android-from-http-using-http) – nKn

+0

하나의 파일에 대해서만 @nKn 동일한 게시물 작업에 DATA + FILE을 게시합니다. –

+0

인코딩 된 base64 파일을 이름 값 쌍. – greenapps

답변

0

@greenapps가 제안했습니다. (신용은 그에게갑니다) 나는 이렇게 풀었습니다.

서버 측에서 파일 내용을 디코딩하고 서버에 수동으로 저장해야하기 때문에 Entirly 해결되지 않았습니다.

그래서 이미했다 BasicNameValuePair에 파일 콘텐츠 추가 jsut :

String fileAsBase64 = Base64.encodeToString(convertToByteArray(mFile) 
       , Base64.DEFAULT); 

    mPostPairs.add(new BasicNameValuePair("filecontent", fileAsBase64)); 

및이 바이트 배열로 변환하는 방법 인 : 서버 측 I에서

/** 
* Reads a file and returns its content as byte array 
* @param file file that should be returned as byte array 
* @return byte[] array of bytes of the file 
*/ 
public static byte[] convertTextFileToByteArray(File file) { 
    FileInputStream fileInputStream = null; 
    byte[] bFile = new byte[(int) file.length()]; 
    try { 
     fileInputStream = new FileInputStream(file); 
     fileInputStream.read(bFile); 
     fileInputStream.close(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } finally { 
     try { 
      fileInputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }finally { 
      fileInputStream = null; 
     } 
    } 
    return bFile; 
} 

이 작업을 수행하십시오 :

$content = imap_base64 ($_POST["filecontent"]); 

콘텐츠를 다시 정상적으로 디코딩합니다.

희망이 다른 사람도

+0

"서버에 수동으로 저장 하시겠습니까?" – greenapps

+0

@greenapps 예, $ _POST 필드가 아닌 $ _POST 필드로 서버에 도착하므로 base64에서 수동으로 디코딩하고 폴더에 저장해야합니다. 이것이 방법이 아니라면, 당신은 무엇을 제안합니까? –

+0

글쎄, PHP 코드를 작성하는 것이 "수동으로하는 것"으로 분류된다는 것은 놀랍다. – greenapps