2010-07-01 2 views
1

자바에서 매우 간단한 파일 업로드 메커니즘이 있습니다. 방금 파일을 가져 와서 서버에 저장합니다. 셀렌으로이 간단한 코드를 테스트 중이며 셀레늄 테스트에서 시간 초과가 발생하면 tomcat_home/work/Catalina/localhost/uploadServlet/디렉토리 아래에 0 바이트 파일이 MultiPart * 파일로 생성됩니다. 장치에 디스크 공간이 남아 있지 않을 때까지 수천 개의 파일을 만듭니다. 이 문제의 원인은 무엇입니까? 이 문제를 어떻게 해결할 수 있습니까? 누구도 이것에 대한 아이디어가 있습니까?톰캣은 0 바이트 파일을 만듭니다.

내 환경은 : 우분투 - 8.04 서버, 아파치 톰캣 - 5.5.29, 태양 자바 1.6

감사합니다, 여기에

내가

String strFileName = request.getParameter("FileName"); 
    String strPath = request.getParameter("Path"); 
    File fFile = (File) request.getAttribute("Content"); 

    int index = strPath.length() - 1; 
    if (strPath.charAt(index) != '/') { 
     strPath += "/"; 
    } 
    if (! new File(strPath).exists()) { 
     new File(strPath).mkdirs(); 
    } 
    File file = new File(strPath + strFileName); 
    FileOutputStream fileOutputStream = new FileOutputStream(file); 
    FileInputStream fileInputStream = new FileInputStream(fFile); 

    byte[] bBuf = new byte[1024]; 

    int iBufLen = 0; 
    int iReadLen = 1024; 
    int iTotelLen = 0; 
    /*read 1024 bytes at a time*/ 
    while ((iBufLen = fileInputStream.read(bBuf)) != -1) { 
     fileOutputStream.write(bBuf); 
     fileOutputStream.flush(); 
     iTotelLen += iBufLen; 
     if (fileInputStream.available() < iReadLen) { 
      iReadLen = fileInputStream.available(); 
      break; 
     } 
    } 

    byte[] tempbBuf = new byte[iReadLen]; 
    fileInputStream.read(tempbBuf, 0, iReadLen); 

    fileOutputStream.write(tempbBuf); 

    fileOutputStream.close(); 
    fileInputStream.close(); 

    if (fFile.exists()) { 
     fFile.delete(); 
    } 
+0

실제 코드를 게시하여 도움을주세요 : –

+0

파일 작성이 끝나면 'OutputStream' 또는'Writer'에서'flush()'및/또는'close() '를 호출 하시겠습니까? – Jesper

+0

나는 게시물을 편집하고 간단한 코드를 삽입했습니다. –

답변

1

나는 apache commons file upload 클래스를 사용했으며 finally 섹션의 임시 파일을 삭제했습니다. 문제는이 구현으로 해결됩니다.

0

그것을가 사용하는 코드 조각입니다 이 코드의 중간에 일종의 예외가 발생했을 가능성이 있습니까? finally 블록의 스트림을 닫는 것이 가장 좋습니다.

+0

나는 이것을 시도했지만 결과는 수많은 0 바이트를 변경하지 않습니다. –

+0

request.getAttribute ("Content")에 내용이있는 항목이 있는지 확실히 알고 있습니까? 이 코드 부분은 빠져있다. –

관련 문제