0
POST 요청을 처리하기 위해 Apache HttpCore HttpAsyncRequestHandler를 사용하고 있습니다. 폼에서 제출 한 매개 변수는 어떻게 얻을 수 있습니까?HttpAsyncRequestHandler에서 게시 매개 변수를 가져 오는 방법
POST 요청을 처리하기 위해 Apache HttpCore HttpAsyncRequestHandler를 사용하고 있습니다. 폼에서 제출 한 매개 변수는 어떻게 얻을 수 있습니까?HttpAsyncRequestHandler에서 게시 매개 변수를 가져 오는 방법
핸들러에서 게시물 본문을 사용할 수 있습니다. EntityUtils.toByteArray(httpRequest.getEntity())
은 전체 데이터를 제공합니다.
나는이 모든 구문 분석 유틸리티를 찾지 못했습니다. params 및 업로드 된 파일을 가져올 수있는 사용자 정의 파서를 작성했습니다.
아래 코드는 params 맵의 매개 변수와 출력 파일 내용을 가져옵니다. 모든 데이터가 메모리에 있으므로 거대한 파일의 경우 충돌이 발생합니다. (경고 : WIP)
InputStream inputStream = new ByteArrayInputStream(data);
ByteArrayOutputStream output = new ByteArrayOutputStream();
HashMap<String, String> params = new HashMap<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String currentLine = reader.readLine();
String boundary = currentLine;
while(reader.ready()) {
currentLine = reader.readLine();
if (currentLine.contains("Content-Disposition: form-data;")) {
String paramName = currentLine;
paramName = paramName.replace("Content-Disposition: form-data; name=","").replace("\"","");
StringBuilder paramValue = new StringBuilder();
if(paramName.contains("filename")) {
String[] temp = paramName.split(";");
paramName = temp[0];
paramValue.append(temp[1].replace("filename", "").replace("=", "").replace(" ", ""));
} else {
currentLine = reader.readLine();
while(!currentLine.contains(boundary)) {
paramValue.append(currentLine);
currentLine = reader.readLine();
}
}
params.put(paramName, paramValue.toString());
}
if (currentLine.contains("Content-Type: ")) {
// File Content here
reader.readLine();
String prevLine = reader.readLine();
currentLine = reader.readLine();
//writing the data to a output stream
while (true) {
if (currentLine.contains(boundary)) {
output.write(prevLine.getBytes());
break;
}
else {
output.write(currentLine.getBytes());
}
prevLine = currentLine;
currentLine = reader.readLine();
}
}
}
시도한 것을 공유하십시오. – J0e3gan
@stokito : 요청 엔터티의 형식은 무엇입니까? URL로 인코딩 된 양식? Multipart from (MIME)? – oleg