commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roland Schär <>
Subject Struts & Fileupload not working
Date Mon, 27 Feb 2006 20:47:07 GMT
I've some problems getting fileupload to work within my Struts 
environment. I already tried to localize the misconfiguration but i had 
no luck so far...all i know is that the while loop does not run at all.
Hope someone of you can help me out.

Here is a part of my action method i use:


import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

public class ProjectAction extends DispatchAction{
    private static Logger logger = Logger.getLogger(ProjectAction.class);
    private HttpSession httpsession;
    public ActionForward mtdUpload(ActionMapping mapping, ActionForm 
form, HttpServletRequest request, HttpServletResponse response) throws 
Exception {
        httpsession = request.getSession(true);

        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = upload.parseRequest(request);
        Iterator iter = items.iterator();
        while(iter.hasNext()) {
            logger.debug("Iterating through form items...");
            FileItem item = (FileItem);
            if (item.isFormField()) {
                //process Form Field
                logger.debug("Process form field: " + item.getFieldName());
                //process Uploaded File
                String fileName = item.getName();
                logger.debug("Process file: " + fileName);
                File file = new File("C:\\Temp\\"+fileName);
                httpsession.setAttribute("projectFile","File (" + 
fileName + ") successfully uploaded.");
        return (mapping.findForward("frmUpload"));


Kind regards
Roland Schär

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message