0

我正在使用 gwt 小部件 gwtupload.client.Uploader,并且我正在尝试使用 fileupload 流 api 将文件保存到数据库中的 blob 列中。问题是,如果文件大于 3k,则只能保存 3k(以及 3.25K)。谢谢您的帮助。这是代码:

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;
}

保存文件的 lina 是:

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

在到达保存 InputStream 的代码之前有 4 种方法(未触及 InputStream):

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();

感谢您的帮助。

4

1 回答 1

0

经过一些工作,我解决了它。

在 storeBlob 方法中,我不能使用 pInputStream.available()。所以,这是我使用的线路:

ps.setBinaryStream(1, pInputStream);
于 2012-12-06T19:35:53.607 回答