1 답변

  • 좋아요

    1

    싫어요
    채택 취소하기
    // 이미지
    Bitmap bitmap = myView.getBitmap();
    
    // 기타 필요한 내용
    String attachmentName = "bitmap";
    String attachmentFileName = "bitmap.bmp";
    String crlf = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    
    // request 준비
    HttpURLConnection httpUrlConnection = null;
    URL url = new URL("http://example.com/server.cgi");
    httpUrlConnection = (HttpURLConnection) url.openConnection();
    httpUrlConnection.setUseCaches(false);
    httpUrlConnection.setDoOutput(true);
    
    httpUrlConnection.setRequestMethod("POST");
    httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
    httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
    httpUrlConnection.setRequestProperty(
        "Content-Type", "multipart/form-data;boundary=" + this.boundary);
    
    // content wrapper시작
    DataOutputStream request = new DataOutputStream(
        httpUrlConnection.getOutputStream());
    
    request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
    request.writeBytes("Content-Disposition: form-data; name=\"" +
        this.attachmentName + "\";filename=\"" + 
        this.attachmentFileName + "\"" + this.crlf);
    request.writeBytes(this.crlf);
    
    // Bitmap을 ByteBuffer로 전환
    byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight()];
    for (int i = 0; i < bitmap.getWidth(); ++i) {
        for (int j = 0; j < bitmap.getHeight(); ++j) {
            //we're interested only in the MSB of the first byte, 
            //since the other 3 bytes are identical for B&W images
            pixels[i + j] = (byte) ((bitmap.getPixel(i, j) & 0x80) >> 7);
        }
    }
    request.write(pixels);
    
    // content wrapper종료
    request.writeBytes(this.crlf);
    request.writeBytes(this.twoHyphens + this.boundary + 
        this.twoHyphens + this.crlf);
    
    // buffer flush
    request.flush();
    request.close();
    
    // Response받기
    InputStream responseStream = new 
        BufferedInputStream(httpUrlConnection.getInputStream());
    BufferedReader responseStreamReader = 
        new BufferedReader(new InputStreamReader(responseStream));
    String line = "";
    StringBuilder stringBuilder = new StringBuilder();
    while ((line = responseStreamReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
    }
    responseStreamReader.close();
    String response = stringBuilder.toString();
    
    
    //Response stream종료
    responseStream.close();
    
    // connection종료
    httpUrlConnection.disconnect();
    
    
    

    출처

답변을 하려면 로그인이 필요합니다.

Hashcode는 개발자들을 위한 무료 QnA 사이트입니다. 계정을 생성하셔야만 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)

ᕕ( ᐛ )ᕗ
로그인이 필요합니다

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 계정을 생성하셔야만 글을 작성하실 수 있습니다.