2015-01-18 5 views

답변

0

핸들러에서 게시물 본문을 사용할 수 있습니다. 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(); 
       } 

      } 
     } 
관련 문제