2011-12-15 4 views
0
public void execute(HttpServletRequest request) throws Exception { 
    DiskFileItemFactory factory = new DiskFileItemFactory(); 

    factory.setSizeThreshold(1*1024*1024*1024); //1 MB 
    /* 
    * Set the temporary directory to store the uploaded files of size above threshold. 
    */ 
    factory.setRepository(new File("c:\\temp")); 

    // Create a new file upload handler 
    ServletFileUpload upload = new ServletFileUpload(factory); 

    // Parse the request 
    List items = upload.parseRequest(request); 
    Iterator iter = items.iterator(); 

    while (iter.hasNext()) { 
     FileItem item = (FileItem) iter.next(); 
     if (item.isFormField()) { 
      InputStream uploadedStream = item.getInputStream(); 
      try { 
       File f = new File("C:\\temp\\index.jpg"); 
       item.write(f); 
       uploadedStream.close(); 
      } 
      catch (IOException e) { 
      } 
     } 
    } 

HTML 양식 :아파치 공용 라이브러리 파일 업로드

<form enctype="multipart/form-data" method="POST" action="<%=request.getContextPath()%>/main?cmd=ci"> 
    <table class = "lineable"> 
     <tr> 
      <td><input type="file" name="file1"/></td> 
      <td><input type="submit" name="q" value="import"/></td> 
     </tr> 
    </table> 
</form> 

내가 index.jpg을 만들 안에 저장하지만, JPG 파일로 제출 '버튼을 어떤 값을 "가져 오기"단어를 씁니다. 뭐가 잘못 되었 니. 감사. 여기

+0

'1 * 1024 * 1024 * 1024'는'1 MB'가 아니라'1 MB'입니다. –

답변

0

적어도 하나의 문제 :

if(item.isFormField()){ 
    InputStream uploadedStream = item.getInputStream(); 
    ... 
} 

당신이 양식 필드에서 찾고 있다면, 왜 당신은 그것의 InputStream 봐야한다? 비 형식 필드 항목, 즉 파일에만 관심을 가져야합니다.

두 번째 검사에서 Commons IO를 사용 중이며 InputStream도 볼 필요가 없습니다. 수표를 무시하면 괜찮을거야.

+0

내가 할 때 효과가있었습니다 (! item.isFormField()) – Elbek

+0

실제로 어딘가에서 코드를 복사했습니다. , 그럼 정확히 저장하지 않았고, 나는 "!"을 찾을 수 없었습니다. 표. 그것은 효과가 있었다. 감사 – Elbek

관련 문제