2013-01-03 1 views
0

Apache File Upload로 파일을 저장하려고합니다. 내 JSP 지금, 아래의 코드를 사용하여 BLOB 값으로 파일을 저장하기 위해 노력하고업로드 된 파일이 자바를 사용하는 Google 애플리케이션 엔진에 BLOB로 저장되지 않습니다.

<form action="/upload" method="post" enctype="multipart/form-data"> 
<input type="file" name="file" /> 
<input type="submit"value="upload" /> 
</form> 
i는 다음과 같이 FIE 업로드 얻을 수있는 내 서블릿에서

,

FileService fileService = FileServiceFactory.getFileService(); 
AppEngineFile file = fileService.createNewBlobFile(mime,fileName); 
boolean lock = true; 
byte[] b1 = new byte[BUFFER_SIZE]; 
int readBytes1 = is.read(b1, 0, BUFFER_SIZE); 
while (readBytes1 != -1) { 
writeChannel.write(ByteBuffer.wrap(b1, 0, BUFFER_SIZE));} 
writeChannel.closeFinally(); 

, 다음과 같은

String blobKey = fileService.getBlobKey(file).getKeyString(); 
Entity Input = new Entity("Input"); 
Input.setProperty("Input File", blobKey); 
datastore.put(Input); 

내가이 파일 이름을 blob 키를 저장할 수 있지만 파일을 저장할 수 없습니다. Blob 뷰어에 "0"바이트가 표시됩니다. & Google App Engine의 Blob 목록입니다.

이 친절하게이 문제를 해결하기 위해 나에게 아이디어를 제안,

당신의 도움이 감사합니다.

내 서블릿

public class UploadServlet extends HttpServlet{ 
    private static final long serialVersionUID = 1L; 
    private static int BUFFER_SIZE =1024 * 1024* 10; 
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
    ServletFileUpload upload = new ServletFileUpload(); 
    FileItemIterator iter; 
try { 
iter = upload.getItemIterator(req); 
while (iter.hasNext()) { 
    FileItemStream item = iter.next(); 
    String fileName = item.getName(); 
    String mime = item.getContentType(); 

    InputStream is = new BufferedInputStream(item.openStream()); 
    try { 
     boolean isMultipart = ServletFileUpload.isMultipartContent(req); 
     if(!isMultipart) { 
      resp.getWriter().println("File cannot be uploaded !");} 
     else { 
      FileService fileService = FileServiceFactory.getFileService(); 
      AppEngineFile file = fileService.createNewBlobFile(mime,fileName); 
      boolean lock = true; 
      FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 
      byte[] b1 = new byte[BUFFER_SIZE]; 
      int readBytes1; 
      while ((readBytes1 = is.read(b1)) != -1) { 
       writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));} 
       writeChannel.closeFinally(); 
      String blobKey = fileService.getBlobKey(file).getKeyString(); 
      Entity Input = new Entity("Input"); 
      Input.setProperty("Input File", blobKey); 
      datastore.put(Input);}} 
catch (Exception e) { 
     e.printStackTrace(resp.getWriter());} 
    } 
} 

답변

3

당신은 잘못된 방법으로 입력 스트림에서 데이터를 읽고있다. 그것은해야한다 :

byte[] b1 = new byte[BUFFER_SIZE]; 
int readBytes1; 
while ((readBytes1 = is.read(b1)) != -1) { 
     writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes)); 
} 
writeChannel.closeFinally(); 

업데이트 : 당신은 여러 부분이 제대로 처리되지 않습니다 - 그것은, 당신은 당신이 (이름 "파일"과 일부) 올바른 부분을 읽을 수 있는지 확인하기 위해 여러 부품을 필요가있다 :

public class UploadServlet extends HttpServlet { 
private static final long serialVersionUID = 1L; 
private static int BUFFER_SIZE = 1024 * 1024 * 10; 

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
    ServletFileUpload upload = new ServletFileUpload(); 

    boolean isMultipart = ServletFileUpload.isMultipartContent(req); 
    if (!isMultipart) { 
     resp.getWriter().println("File cannot be uploaded !"); 
     return; 
    } 

    FileItemIterator iter; 
    try { 
     iter = upload.getItemIterator(req); 
     while (iter.hasNext()) { 
      FileItemStream item = iter.next(); 
      String fileName = item.getName(); 
      String fieldName = item.getFieldName(); 
      String mime = item.getContentType(); 

      if (fieldName.equals("file")) { // the name of input field in html 
       InputStream is = item.openStream(); 
       try { 
        FileService fileService = FileServiceFactory.getFileService(); 
        AppEngineFile file = fileService.createNewBlobFile(mime, fileName); 
        boolean lock = true; 
        FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 
        byte[] b1 = new byte[BUFFER_SIZE]; 
        int readBytes1; 
        while ((readBytes1 = is.read(b1)) != -1) { 
         writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1)); 
        } 
        writeChannel.closeFinally(); 
        String blobKey = fileService.getBlobKey(file).getKeyString(); 
        Entity input = new Entity("Input"); 
        input.setProperty("Input File", blobKey); 
        datastore.put(input); 
       } catch (Exception e) { 
        e.printStackTrace(resp.getWriter()); 
       } 
      } 
     } 
    } catch (FileUploadException e) { 
     // log error here 
    } 
} 
} 
+0

나는 그것을 시험해 보았지만 파일이 아니라 블롭 키만을 저장했다. 이걸 도와 줄 수 있습니까? – sathya

+0

양식을 사용하여 파일을 업로드합니까? POST를 처리하는 서블릿의 전체 코드를 게시해야합니다. –

+0

지금 내 서블릿 파일을 게시했는지 확인할 수 있습니까 – sathya

관련 문제