0

내 앱에 이미지 업로드 기능을 추가하고 싶습니다. 문제는 서버에 큰 크기의 이미지를 업로드하고 싶다는 것입니다. 그 이유는이 시나리오에서 Multipart 접근법을 사용하고자하는 이유입니다. 내가이 개념을 처음 접했을 때 나는 이것을 구현하는 방법을 모른다. 또한 이미지 업로드와 관련된 몇 가지 구체적인 질문이 있습니다.Android에서 이미지 업로드를 위해 MultiPart 방식을 구현하는 방법

나는 예를 들어, 내가 JSON 데이터를 전송해야하는 서버에 POST 요청을 보낼 필요가 : -

여기
JSONObject jsonObject = new JSONObject(); 

     jsonObject.put("filename", "menu.jpg"); 
     jsonObject.put("filetype", "image/jpeg"); 
     jsonObject.put("user_id", "1"); 
     jsonObject.put("user_file", ""); 

는, 사용자 파일이 실제 I 업로드 할 파일과입니다 위의 매개 변수는이 이미지 파일과 함께 서버로 보내야하며 파일을 업로드하는 데 필수입니다.

이제 제 질문은 MultiPart 접근 방식을 사용하여 JSON을 보낼 수 있으며 전체 단계를 수행하기 위해 Volley 또는 AsyncTask를 사용해야하는 것입니다. 누구든지 도움을 줄 수 있습니다. 감사

답변

0

당신이 무엇이든을 보낼 수있는 이미지 대신에 응답

httpPost.setEntity(entity); 
HttpResponse response = httpClient.execute(httpPost); 

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 



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

if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
{ 
    return s.toString(); 
}else 
{ 
    return "{\"status\":\"false\",\"message\":\"Some error occurred\"}"; 
} 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

을 받고이

public void connectForMultipart() throws Exception { 
    con = (HttpURLConnection) (new URL(url)).openConnection(); 
    con.setRequestMethod("POST"); 
    con.setDoInput(true); 
    con.setDoOutput(true); 
    con.setRequestProperty("Connection", "Keep-Alive"); 
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
    con.connect(); 
    os = con.getOutputStream(); 
} 

public void addFormPart(String paramName, String value) throws Exception { 
    writeParamData(paramName, value); 
} 

public void addFilePart(String paramName, String fileName, byte[] data) throws Exception { 
    os.write((delimiter + boundary + "\r\n").getBytes()); 
    os.write(("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes()); 
    os.write(("Content-Type: application/octet-stream\r\n" ).getBytes()); 
    os.write(("Content-Transfer-Encoding: binary\r\n" ).getBytes()); 
    os.write("\r\n".getBytes()); 

    os.write(data); 

    os.write("\r\n".getBytes()); 
} 
public void finishMultipart() throws Exception { 
    os.write((delimiter + boundary + delimiter + "\r\n").getBytes()); 
} 

처럼 일해야, 당신이하고 싶습니다 ... 그 경우 몇 가지 도대체를 생성 후 당신은 작은 부분으로 다음 send.And 서버에 작은 부분을 가입하려고 시도 큰 파일을 나눠해야합니다.