2012-05-12 2 views
2

의 형식 enctype = "multipart/form-data"양식이 multipart/form-data로 인코딩 된 상태에서 서블릿에서 값을 가져 오는 방법.양식 텍스트 상자 및 이미지 값 양식을 얻으려면 java

<FORM name="filesForm" action="FileUpload" method="post" enctype="multipart/form-data"> 
<input type="text" name="firstname"> 
<input type="text" name="lastname"> 
<input type="file" name="filename"> 
</form> 

서블릿 이름, 성 및 파일 값에서 이러한 값을 얻는 방법.

+0

내가 추측이 질문의 중복 등이 있습니다를 . 좀 찾아 보자 :) –

+0

좋습니다. http://stackoverflow.com/questions/5857626/unable-to-read-form-field-in-servlet 읽어 보면 도움이 될 것입니다. –

+0

다른 하나 : http://stackoverflow.com/questions/10551431/upload-image-from-android-to-java-servlet-and-save-it. HttpServletRequest.getPart()는 필요한 것입니다. 이것은 다중 요청을 처리합니다. – home

답변

3

당신은 아파치 평민 파일 업로드를 사용할 수 있습니다 http://commons.apache.org/fileupload/

일반적으로 같은 모양 코드 :

import org.apache.commons.fileupload.*; 

...

DiskFileUpload upload = new DiskFileUpload(); 
List<FileItem> items = upload.parseRequest(request); // request is HttpServletRequest 

// iterate through form fields 
for(FileItem item:items) { 
    if(item.isFormField()) { // text fields, etc... 
     String fieldName = item.getFieldName(); 
     String value = item.getString(); 
    } else { // file fields 
     item.write(new File("some/path")); 
    } 
}