PDA

View Full Version : FileUpload



FireGlow
26 May 2009, 8:11 AM
Ups I was in the wrong forum :x

I found an example for an Servlet to handle a FileUpload:


public class ArchetypeUploadServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private ArchetypeService archetypeService;

public ArchetypeUploadServlet() {
super();

}

public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException {
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

try {
archetypeService = ArchetypeServiceImpl.getInstance();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
items = upload.parseRequest(req);
Iterator it = items.iterator();
File uploadedFile;
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
GregorianCalendar tempDate = new GregorianCalendar();
uploadedFile = new File("D:/server/temp/"
+ tempDate.getTimeInMillis() + ".adl");

item.write(uploadedFile);

User user = (User) req.getSession().getAttribute(
User.USER_LOGGED_IN);
if (user == null) {
throw new AccessControlException(
"Kein User in der Session gefunden.");
}
System.out.println(user);
System.out.println(user.getLogin());
archetypeService.importArchetype(uploadedFile, user);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}I registered it in my web.xml and modul.xml!

My relevant formcode is this:


this.setAction(GWT.getModuleBaseURL() + "ArchetypeUploadServlet");
this.setEncoding(Encoding.MULTIPART);
this.setMethod(Method.POST);

[...]

public void doUpload()
{
if(this.isValid())
{
// //TODO: Fieldbinding would be better, but doesn't work that easily
// this.archetypeDataModel.setFilePath((String) archetypeFile.getValue());
// Dispatcher.get().dispatch( new EHRflexEvent<ArchetypeDataModel>( EHRflexEventType.ARCHETYPE_IMPORT_SUBMIT, this.archetypeDataModel) );
new MessageDialog(this.getAction());
this.submit();
}
}

It works fine so far!

But I'm wondering how I can react on a successful uploading of the file? I don't have any callback or something! Is there a mechanism to react of a successful submit() ?

FireGlow
26 May 2009, 11:05 AM
Hey...

I finally got it, but maybe someone is interested in the solution:

You can add a FormEvent Listener on the Events.Submit!

This FormEvent object has a returnHTML attribute that has everything you write into the HttpResponse, so you can validate that and do an action!