2012-11-26 1 views
3

내 서버에 데이터를 게시하는이 코드가 : 코드는 완벽하게 작동여러 부분, 한 부분의 설정 Content-Type을

// HTTP Settings 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost postRequest = new HttpPost(
        "http://myserver.com/Login"); 
      MultipartEntity reqEntity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE); 

      // Http Headers 
      postRequest.addHeader("Accept", "application/xml"); 
      postRequest.addHeader("Connection", "keep-alive"); 

      // Credentials 
      reqEntity.addPart("username", new StringBody(ServerData.username)); 
      reqEntity.addPart("password", new StringBody(ServerData.password)); 

      if (m_sigFile.exists()) { 
       Bitmap m_sig = BitmapFactory.decodeFile(sigFilePath 
         + "m_sig.jpg"); 
       ByteArrayOutputStream m_bao = new ByteArrayOutputStream(); 
       m_sig.compress(Bitmap.CompressFormat.JPEG, 90, m_bao); 

       byte[] m_ba = m_bao.toByteArray(); 
       String m_ba1 = Base64.encodeToString(m_ba, 0); 
       reqEntity.addPart("m_sig.jpg", new StringBody(m_ba1)); 
      } 

      postRequest.setEntity(reqEntity); 
      HttpResponse response = httpClient.execute(postRequest); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        response.getEntity().getContent(), "UTF-8")); 
      String sResponse; 
      StringBuilder s = new StringBuilder(); 

      while ((sResponse = reader.readLine()) != null) { 
       s = s.append(sResponse); 
      } 

는 모든 데이터가 jpeg 파일을 제외한 서버로 전송됩니다. 콘텐츠 유형을 'image/jpeg'로 설정 한 경우에만 서버가 파일을 승인하지만 이미지의 경우에만 허용됩니다. 사용자 이름과 암호는 일반 텍스트 여야합니다. 이것이 가능한가?

new StringBody(titleString, "application/atom+xml", Charset.forName("UTF-8")); 

답변

2

이 작동합니다 :

  ContentBody cbFile = new FileBody(new File(myPath 
        + "image_1.jpg"), 
        "image/jpeg"); 
      reqEntity.addPart("photo1"), cbFile); 

당신이 존재하는 파일 여부를 확인하는 것을 잊지 마십시오 콘텐츠 형식을 받아 StringBody에 대한 생성자가있다

1

!