2012-11-09 4 views
2

나는 우리의 애플 리케이션에서 파일을 http로 게시 방법을 통해 끊어 지길 원한다. 됐었 빈 파일이 서버에 생성하고있다sd 카드에서 http 게시로 파일을 보내는 방법은 무엇입니까?

 HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    DataInputStream inStream = null; 
    String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() 
      + "/TamTrack/TamTrackDetails.xml"; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 
    String responseFromServer = ""; 
    String urlString = "http://192.158.1.7/Geo/myfilename"; 
    try 
    { 
     //------------------ CLIENT REQUEST 
    FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 
     // open a URL connection to the Servlet 
     URL url = new URL(urlString); 
     // Open a HTTP connection to the URL 
     conn = (HttpURLConnection) url.openConnection(); 
     // Allow Inputs 
     conn.setDoInput(true); 
     // Allow Outputs 
     conn.setDoOutput(true); 
     // Don't use a cached copy. 
     conn.setUseCaches(false); 
     // Use a post method. 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
     dos = new DataOutputStream(conn.getOutputStream()); 
     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd); 
     dos.writeBytes(lineEnd); 
     // create a buffer of maximum size 
     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 
     // read file and write it into form... 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     //while (bytesRead > 0) 
     Log.v("info",".size."+bytesRead); 
     for(int n1=0;n1<bytesRead;n1++) 
     { 


     dos.write(buffer, 0, bufferSize); 
     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

     } 

     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
     // close streams 
     Log.v("info","File is written"); 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 
    } 
    catch (MalformedURLException ex) 
    { 
      Log.v("info", "error: " + ex.getMessage(), ex); 
    } 
    catch (IOException ioe) 
    { 
      Log.v("info", "error: " + ioe.getMessage(), ioe); 
    } 
    //------------------ read the SERVER RESPONSE 
    try { 
      inStream = new DataInputStream (conn.getInputStream()); 
      String str; 

      while ((str = inStream.readLine()) != null) 
      { 
       Log.v("info","Server Response "+str); 
      } 
      inStream.close(); 

    } 
    catch (IOException ioex) 
    { 
      Log.v("info", "error: " + ioex.getMessage(), ioex); 
    } 
    return null; 

을 휴한지로 내 코드처럼 찾고

.

아무도 해결책을 알고 있다면 제발 도와주세요.

미리 감사드립니다.

+1

서버 측 코드를 업로드하십시오. – Lucifer

+0

어떤 파일 유형입니까? –

답변

0

시도 this 코드입니다. 그것은 당신이 쉽게 파일을 보낼 수있는 "SimpleMultipartEntity"클래스를 사용하고 있습니다.

0

나는 아파치 Http 커먼을 사용하는 것을 선호한다. 그것은 안드로이드의 일부이다. 나는 이것이 훨씬 간단하고 깨끗한 접근법이라고 생각한다. 다음과 같이해야합니다 :

AndroidHttpClient client = AndroidHttpClient.newInstance("useragent string"); 
URI uri = "something"; 
File file = new File("/tmp/data"); 
HttpPost post = new HttpPost(uri); 
post.setEntity(new FileEntity(file, "text/html")); 
HttpResponse httpResponse = client.execute(post); 
int statusCode = httpResponse.getStatusLine().getStatusCode(); 
관련 문제