마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

httpURLConnection을 이용한 이미지와 text 한번에 전송

0 추천

서버로 이미지와 텍스트 2개를 한번에 보내려고 합니다. 

일단 아래는 텍스트 전송 부분까지의 코드입니다.


String absolutePath;
int maxBufferSize = 1 * 1024 * 1024;
int bufferSize;
String crlf = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";

public String doInBackground(String... params){

    try {
        URL url = new URL(params[0]);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream ds = new DataOutputStream(conn.getOutputStream());
        FileInputStream mFileInputStream = new FileInputStream(absolutePath);


        // 일반 텍스트 전송(String) 1
        // 같은 형태로 계속해서 데이터 추가
        ds.writeBytes(twoHyphens + boundary + this.crlf);
        ds.writeBytes("Content-Disposition: form-data; name=\"bucketName\"" + this.crlf);
        ds.writeBytes(this.crlf);
        ds.writeBytes("heesu1220-nate-com" + this.crlf);

        // 일반 텍스트 전송(String) 2
        ds.writeBytes(twoHyphens + boundary + this.crlf);
        ds.writeBytes("Content-Disposition: form-data; name=\"plantName\"" + this.crlf);
        ds.writeBytes(this.crlf);
        ds.writeBytes("dddddddddddddaaaaaaaaaaa" + this.crlf);

        // 일반 텍스트 전송(String) 2
        ds.writeBytes(twoHyphens + boundary + this.crlf);
        ds.writeBytes("Content-Disposition: form-data; name=\"comments\"" + this.crlf);
        ds.writeBytes(this.crlf);
        ds.writeBytes("ddddd" + this.crlf);

 

absolutePath 스트링 변수에 사진첩 이미지의 절대경로가 들어있습니다. 

이것을 이용하여 갤러리 이미지도 추가 전송할수있는 방법이 있을까요?

익명사용자 님이 2016년 12월 23일 질문

1개의 답변

0 추천
일반적으로 이미지 데이터 등은 멀티파트 메세지를 만들어 전송을 하긴 하지만.

아래와 같은 순서로 텍스트로 전송하시는 것도 가능합니다.

1. 갤러리 이미지 경로를 구해 데이터를 read

2. 1번을 통해 읽은 값을 base64인코딩을 하여 text 메세지 생성

3. 2번에서 생성한 text 메세지 전송

4. 받은 곳에서 3번 text 메세지를 받아 base64 디코딩한 후 파일로 생성,
익명사용자 님이 2016년 12월 23일 답변
...