users@glassfish.java.net

Re: pls help, file upload to a local directory

From: <glassfish_at_javadesktop.org>
Date: Thu, 19 Apr 2007 09:37:31 PDT

here is my code


String DESTINATIONDIR = "e://temp//";

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = null;
    try {
           items = upload.parseRequest(request);
    } catch (FileUploadException e) {
           e.printStackTrace();
    }
    
    Iterator itr = items.iterator();
    while (itr.hasNext()) {
        FileItem item = (FileItem) itr.next();
        if (item.isFormField()) {
        } else {
            try {
                   String itemName = item.getName();
                   File savedFile = new File(DESTINATIONDIR+itemName);
                   item.write(savedFile);
                   
                    out.println("<tr><td><b>Your file has been saved at the loaction:</b></td></tr><tr><td><b>"+DESTINATIONDIR+"\\"+itemName+"</td></tr>");

            } catch (Exception e) {
                   e.printStackTrace();
            }
        }
    }
}
[Message sent by forum member 'arckjmaster' (arckjmaster)]

http://forums.java.net/jive/thread.jspa?messageID=213219