I'm using a gwt widget gwtupload.client.Uploader, and i'm trying to save the file into a blob column in a database using fileupload streaming api. The problem is that if the file is bigger than 3k only saves 3k (well 3.25K). Thanks for the help. Here is the code:
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;
}
The lina that saves the file is :
uploadService.sauvegarder((Fichier) o, stream);
And there are like 4 methods after until reach the the code to save the InputStream (the InputStream is not touched):
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);
}
}
If I use the FileItemFactory it worked, but that's not what I want:
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> uploadedItems = upload.parseRequest(request);
for (FileItem item : uploadedItems) {
InputStream stream = item.getInputStream();
Thank you for your help.
After some work around, I solved it.
in the storeBlob method i can't use the pInputStream.available(). So, this is the line i used:
ps.setBinaryStream(1, pInputStream);