Hi,
I use a FormPanel to upload a file and cause I can't fix the maximum size of the file in the client, I calculate it in the servlet and throw a new javax.xml.ws.http.HTTPException(400) if the file's size is larger than 5000000.
The problem is that nothing happens.
There's some code.
In the client:
Code:
public class UploadPanel extends LayoutContainer {
private final Messages messages = GWT.create(Messages.class);
private MyFileUploadField myFileUploadField;
private HiddenField<Long> idTask = new HiddenField<Long>();
private HiddenField<Long> idPatient = new HiddenField<Long>();
@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
setStyleAttribute("margin", "10px");
final FormPanel panel = new FormPanel();
panel.setHeading(messages.ajouterUnFichier());
panel.setFrame(true);
panel.setAction("uploadFile");
panel.setEncoding(Encoding.MULTIPART);
panel.setMethod(Method.POST);
panel.setButtonAlign(HorizontalAlignment.CENTER);
panel.setWidth(350);
myFileUploadField = new MyFileUploadField();
myFileUploadField.setAllowBlank(false);
myFileUploadField.setShim(true);
myFileUploadField.setName("uploadedfile");
myFileUploadField.setFieldLabel(messages.fichier());
panel.add(myFileUploadField);
Button btn = new Button(messages.Save());
btn.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
if (!panel.isValid()) {
return;
}
idPatient.setValue(5432L);
idTask.setValue(6789L);
panel.submit();
}
});
idPatient.setName("idPatient");
panel.add(idPatient);
idTask.setName("idTask");
panel.add(idTask);
panel.addButton(btn);
add(panel);
}
}
In the Servlet:
Code:
public class UploadServlet extends HttpServlet {
private File tmpDir;
private File destinationDir;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setSizeThreshold(1 * 1024 * 1024); //1 MB
fileItemFactory.setRepository(tmpDir);
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
uploadHandler.setSizeMax(5000000);
File file = null;
FileItem item = null;
try {
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
item = (FileItem) itr.next();
if (item.isFormField()) {
out.println("File Name = " + item.getFieldName() + ", Value = " + item.getString());
} else {
file = new File(destinationDir, item.getName());
file.setWritable(true);
item.write(file);
OmniUser connectedUser = (OmniUser) request.getSession().getAttribute("USER");
String mimeType = this.getServletContext().getMimeType(file.getPath());
//TODO something
}
}
} catch (FileUploadException ex) {
log("Error encountered while parsing the request", ex);
} catch (Exception ex) {
if (ex instanceof HTTPException) {
log("Fichier trop volumineux", ex);
throw new HTTPException(400);
} else {
log("Error encountered while uploading file", ex);
}
} finally {
if (out != null) {
out.close();
}
if (item != null) {
item.delete();
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Must I redirect the page or something like that?
I ask me if it's possible to have a progress bar during the upload?
I use gxt 2.2.4 and gwt 2.3.0.
Thanks a lot for the help.