2012-08-08 2 views
0

내가 서버에 안드로이드 응용 프로그램을 연결할 수있는 응용 프로그램을 만들려고 노력하고있다, 그리고 나는이 코드를 발견 업로드하고 2 개 이상의 파일을 다운로드 할 필요가에서/2 개 이상의 파일을 다운로드업로드 및 서버

private void doFileUpload() { 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 
     String existingFileName = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/android/data/[package]/files/productHistory"; 
     **String existingFileName2 = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/android/data/[package]/files/productStock";** 
     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.168.1.112/johnson/learn/android/"; 
     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("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" 
        + existingFileName2 + "\"" + 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) { 
       dos.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      } 
      // send multipart form data necesssary after file data... 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      // close streams 
      Log.e("Debug", "File is written"); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
     } catch (MalformedURLException ex) { 
      Log.e("Debug", "error: " + ex.getMessage(), ex); 
     } catch (IOException ioe) { 
      Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
     } 
     // ------------------ read the SERVER RESPONSE 
     try { 
      inStream = new DataInputStream(conn.getInputStream()); 
      String str; 

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

     } catch (IOException ioex) { 
      Log.e("Debug", "error: " + ioex.getMessage(), ioex); 
     } 
    } 

굵은 텍스트 , 나에 의해 편집하지만 실패입니다, 그것은 단지 첫 번째

내 질문은 코드 2/더 많은 파일을 업로드하는 방법 1입니다 업로드? 2. 다운로드 방법은 무엇입니까? 사전

+0

코드에서 두 번째 내용 처리 헤더를 추가하지만 출력 스트림에 두 번째 파일을 쓰지 않습니다. 첫 번째 코드는 –

+0

입니다. 시도했지만 작동하지 않습니다. – Sieryuu

+0

연결 출력 스트림, 당신은 단지 첫 번째 파일을 작성하고 있습니다; 당신은 경계 문자열로 구분 된 두 가지 모두를 써야합니다. –

답변

0

특정 서버를 사용하는 경우 확실하지, 아직 Parse에서

덕분에 서버에서/수신 데이터 (당신의 이미지의 경우)을 보내 직관적 인 API와 달콤한 SDK를 제공합니다. 서버와 데이터를 송수신하는 코드를 작성하는 데 많은 시간을 소비하고 싶지 않은 경우 사용해 보시기 바랍니다. (그리고 아니, 나는 파스 (Parse)에서 일하지 않는다. 그들의 도구에 대한 열렬한 팬 : P).

+0

답장을 보내 주셔서 감사합니다. – Sieryuu