2013-03-01 3 views
7

되는 HTML의 형태는자바 서블릿에서 HttpServletRequest의 파일을받는 방법

... 
<form method="post" action="/foobar"> 
    <input type="file" name="attachment" /> 
    <input type="text" name="foo" /> 
    ... other input fields 
</form> 

처럼 그리고 서블릿

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    String attachment = request.getParameter("attachement"); 
    String foo = request.getParameter("foo"); 
    // get other parameters from the request 
    // and get the attachment file 
} 

처럼 될 것입니다 그리고 난

  1. 을 궁금하네요

    HttpServletRequest 개체에서 타사 라이브러리를 사용하여 파일을 가져 오는 방법이 있습니까?

  2. 무엇을 request.getParameter("attachement")이 반환합니까? 파일 이름인가요?

  3. 바이너리 입력을 파일 시스템의 웹 컨테이너 또는 메모리에 임시로 자동 저장합니까?

+3

여기있다 : http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp -servlet/2424824 # 2424824 –

+0

http://stackoverflow.com/questions/3831680/httpservletrequest-get-post-data JSON 데이터를 처리 할 수도 있습니다. 첨부 파일이 URL로 표시되는 경우 HTTPURLConnection을 사용하여 데이터를 가져와야합니다. –

답변

1

anythging 전에 양식 작업은 "POST"이고 enctype = "multipart/form-data"여야합니다.

... 그 파일을 얻으려면 요청을 스스로 준비해야합니다.

당신은 확인해야합니다 : 당신이 컨트롤러 측 당신의 양식 를 제출할 때

Multipart requests/responses java

0

이 형태 = "다중/폼 데이터"지금

<form name="formname" action="servletName" method="post" enctype="multipart/form-data"> 
<input type="file" name="attachment" /> 
    <input type="text" name="foo" /> 
    ... other input fields 
</form> 

에 enctype를 추가 그림을 얻을 수있다

String picture = (request.getParameter("attachment")).getBytes(); 

나는 파일을 이미지라고 가정 했으므로 어떤 파일이라도 넘겨 줄 수있다.

+2

문자열 대신 바이트 배열로 파일을 받아야합니다. 어쨌든 그것조차 컴파일되지 않습니다. 너의 오타가 될지도 모른다. – Manpreet

관련 문제