2012-11-26 78 views
0

我正在使用一個gwt小部件gwtupload.client.Uploader,我試圖將文件保存到使用fileupload流api的數據庫中的blob列。 問題是如果文件大於3k只保存3k(好3.25K)。 感謝您的幫助。 下面是代碼:Apache Commons FileUpload只保存文件的一部分

try { 
    ServletFileUpload upload = new ServletFileUpload(); 
    upload.setProgressListener(listener); 
    FileItemIterator iter = upload.getItemIterator(request); 
    InputStream stream = null; 
    while (iter.hasNext()) { 
     FileItemStream item = iter.next(); 
     String name = item.getFieldName(); 
     stream = item.openStream(); 
     Object o = getThreadLocalRequest().getSession().getAttribute(PortailServiceIMPL.FICHIER_SESSION_STORE_KEY); 
     if (o != null && o instanceof Fichier) { 
      uploadService.sauvegarder((Fichier) o, stream); 
      listener.update(listener.getContentLength(), 
        listener.getContentLength(), 0); 
     } else { 
      throw new RuntimeException(
        "Impossible de recuperer le fichier de la session."); 
     } 
    } 
    if (stream != null) { 
     stream.close(); 
    } 
} catch (SizeLimitExceededException e) { 
    RuntimeException ex = new UploadSizeLimitException(
      e.getPermittedSize(), e.getActualSize()); 
    listener.setException(ex); 
    throw ex; 
} catch (UploadSizeLimitException e) { 
    listener.setException(e); 
    throw e; 
} catch (UploadCanceledException e) { 
    listener.setException(e); 
    throw e; 
} catch (UploadTimeoutException e) { 
    listener.setException(e); 
    throw e; 
} catch (Exception e) { 
    logger.error("UPLOAD-SERVLET (" + request.getSession().getId() 
      + ") Unexpected Exception -> " + e.getMessage() + "\n" 
      + stackTraceToString(e)); 
    e.printStackTrace(); 
    RuntimeException ex = new UploadException(e); 
    listener.setException(ex); 
    throw ex; 
} 

,節省了文件的麗娜是:

uploadService.sauvegarder((Fichier) o, stream); 

而且還有像後,直到到達的代碼來保存的InputStream(InputStream中未觸及的4種方法):

public void storeBlob(long id, InputStream pInputStream) throws Exception { 
    try { 

     java.sql.Connection conn = //get the connection; 

     PreparedStatement ps = conn.prepareStatement(SQL_STORE); 

     ps.setBinaryStream(1, pInputStream, pInputStream.available()); 
     ps.setLong(2, id); 

     ps.executeUpdate(); 

     ps.close(); 

     em.getTransaction().commit(); 
    } catch (Throwable t) { 
     em.getTransaction().rollback(); 
     throw new Exception(t); 
    } 

} 

如果我用它工作的FileItemFactory,但是這不是我想要的:

FileItemFactory factory = new DiskFileItemFactory(); 
ServletFileUpload upload = new ServletFileUpload(factory); 
List<FileItem> uploadedItems = upload.parseRequest(request); 
    for (FileItem item : uploadedItems) { 
     InputStream stream = item.getInputStream(); 

謝謝你的幫助。

回答

0

經過一番努力,我解決了它。

在storeBlob方法中,我無法使用pInputStream.available()。所以,這是我使用的線:

ps.setBinaryStream(1, pInputStream); 
相關問題