일부 데이터를 읽으려면 $ _GET을 사용하면 잘 업로드되는 안드로이드 사진 업로드 스크립트가 있습니다. 모든 $ _POST를 사용하고 싶습니다. 문제는 파일 업로드와 함께 기본 이름 값 쌍을 포함하도록 게시 된 요청을 데이터에 추가해야한다는 것입니다. 서버에서 $ _GET을 사용하고 URL 문자열에 여분의 데이터를 추가하면이 작업을 수행 할 수 있지만 게시물을 사용하고 싶습니다. $ _POST를 사용하여 서버가 읽을 수있는 파일 업로드의 다른 게시물 데이터를 인코딩하는 방법이 있습니까? Heres 함수, URL 문자열을 어디서, 내가 api_key, session_key 변수 및 POST 요청을 보낼 수있는 메서드를 가져 오는 중입니다.어떻게 안드로이드 HTTP 파일 업로드 POST 요청에 이름 값 데이터를 추가합니까
public void uploadPhoto(FileInputStream fileInputStream, String sessionKey) throws IOException, ClientProtocolException {
URL connectURL = new URL(API_URL +"?api_key=" +API_KEY+ "&session_key=" + sessionKey + "&method=" + ApiMethods.UPLOAD_PHOTO); //server ignores this data
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
HttpURLConnection conn = (HttpURLConnection) connectURL.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);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+ "file.png" + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1028;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int 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);
/*
* dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary +
* twoHyphens + lineEnd);
*/
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush();
// Log.d(TAG, " dos5: " + dos.toString());
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String s = b.toString();
dos.close();
}
편집 나는 다음과 같은 것을 생각하고 각 추가 PARAM 당신에게 시간을 절약 할 수있는 MIME 인코딩을 지원하는 일부 기존의 열린 도서관이 있습니다
dos.writeBytes("--" + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data;name=api_key=" + lineEnd + lineEnd + API_KEY);
dos.writeBytes(lineEnd);
dos.writeBytes("--" + boundary + "--" + lineEnd);
참고 : 절약 할 수 있습니다 내가 게시를 위해 해킹 몇 가지 물건에 여기에서 코드를 사용 아파치 라이브러리보다는 AOSP에서 멀티 파트 클래스의 복사본을 포함시킴으로써 apk에 많은 공간을 확보 할 수 있습니다 : http://android.git.kernel.org/?p=platform/frameworks/base.git;a=tree;f= core/java/com/android/internal/http/multipart; h = 7256a2afc68c0ae712c4114c9dec50333ae9875c; hb = HEAD –
포인터 주셔서 감사합니다! Android Open Source Project를 아직 파헤 치지 않았으므로 리소스를 사용할 수 있는지 확인하십시오. – mikerowehl
@ nEx.Software Apache 웹 사이트에서 다운로드 할 수있는 항목을 가져 오지 않고 MultiPart 클래스를 가져오고 사용할 수있는 방법을 보여주는 자습서를 가르쳐 주시겠습니까? – beerBear