I cannot help you. But I have exactly the same problem. Sometimes it seems that, the execute method is called before the HTTP client has finished uploading the file. This results in a null value for the File object upload.

But the file is uploaded, the problem is just, he the execute methd is called to fast.

I try to use Apache webserver as frontend (by the use of the mod_jk module), and here I have the problem almost the time. Is there some time-out parameters one can set?

I am using JBOSS as Application server (version 4.0.5) and struts 2.0.6.



best regards

Torben Frøberg
Fasanvænget 484
Tlf. privat 49 14 05 85
Tlf. arbejde 45 17 12 97


Kurapica wrote:
Hi all,

I am using struts 2.0.6 and encountered a submit problem.

I have a form with enctype="multipart/form-data" property. there're
other text fields beside <s:file>. when I submit, at most time it
works fine, but I found that occasionally all fields are null. I have
replaced commons-fileupload, commons-io and commons-collections with
latest versions and the problem still exists.

struts version is 2.0.6
jdk version is 1.5.0_11
tomcat version is 5.5.12
commons-fileupload version is 1.2
commons-collections version is 3.2
commons-io version is 1.3.1

I provided jspfile, struts.xml and action class below. If any
infomation required, please reply.

Any suggestion is welcome and thanks for your advice~

This is my jsp file:

----------------------jsp file begin----------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="/WEB-INF/jsp/inc/head_code.jsp" %>
<%if(!isLoggedIn)response.sendRedirect("./Login.jsp");%>
<html>
<head>
<link href="<s:url value='/style.css'/>" rel="stylesheet" type="text/css">
    <link href="<s:url value='/style-kurapica-temp.css'/>"
rel="stylesheet" type="text/css">
   <title>Shoutloud Add Item Page</title>

</head>

<body>
<center>
<s:form action="add_item.action" method="POST" enctype="multipart/form-data">
    <s:property value="message"/>
    <s:bean name="com.shoutloud.ui.beans.ItemTypesBean" id="types">
        <s:select label="Item Type" name="itemType" value="%{1}"
list="#types" required="true" requiredposition="left"  />
    </s:bean>
    <s:textfield label="Item Name" key="name" required="true"
requiredposition="left"/>
    <s:textfield label="Item Desctiption" key="description"/>
    <s:textfield label="Price in US$(e.g.:1.99)" key="price"
required="true" requiredposition="left"/>
<s:file name="upload" label="File" required="true" requiredposition="left"/>
    <tr><td colspan="2" align="center">
   <s:submit theme="simple"/>
   </td></tr>
</s:form>
</center>
</body>
</html>
----------------------jsp file end----------------------

This is part of my struts.xml

----------------------struts.xml begin----------------------
<action name="preadd_item" class="preaddItem">
 <result name="success">/WEB-INF/jsp/add_item.jsp</result>
</action>

<action name="add_item" class="addItem">
 <result name="success" type="redirect-action">show_item</result>
 <result name="input">/WEB-INF/jsp/add_item.jsp</result>
</action>
----------------------struts.xml end----------------------

This is my action file

----------------------action begin----------------------
package com.shoutloud.action.user;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import sun.util.logging.resources.logging;

import com.opensymphony.xwork2.ActionSupport;
import com.shoutloud.model.Item;
import com.shoutloud.model.MusicItem;
import com.shoutloud.service.AddItemService;
import com.shoutloud.service.impl.LoginServiceImpl;

public class AddItemAction extends ActionSupport implements
ServletRequestAware {

    private static final long MAX_UPLOAD_SIZE = 0x2000000L;

    private String itemType;

    private String description;

    private Double price;

    private String url;

    private String name;

    private AddItemService addItemService;

    private HttpServletRequest request;
private String contentType;

    private File upload;

    private String fileName;
    public void setAddItemService(AddItemService addItemService) {
        this.addItemService = addItemService;
    }

public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public String execute() throws Exception {
        if (!LoginServiceImpl.isLoggedIn(request))
            return LOGIN;
        if (itemType == null)
            return INPUT;
        int itemTypeInt = Integer.parseInt(itemType);
        if (itemTypeInt == 1) {
            // This is a Music Item
            Item item = new Item();
            item.setName(getName());
            item.setDescription(getDescription());
            item.setItemType(itemTypeInt);
            item.setPrice(getPrice());
            item.setUrl(getUrl());
            MusicItem musicItem = new MusicItem();
Integer uid = (Integer) request.getSession().getAttribute("uid");
            String relativePath=uid.toString();
            //use url field to save the relative path of item file.
            //this url generating arthmetic may be changed in the future.
            item.setUrl(relativePath+"/"+this.fileName);
            // set properties of Music Item

            // save item and music item
            addItemService.addMusicItem(uid, item, musicItem);
            request.getSession().setAttribute("itemId", item.getId());
            // add uploader

            String rootPath =
ServletActionContext.getServletContext().getRealPath("/resources");
String uploadDir = (new StringBuilder()).append(rootPath).append("/").append(
                    relativePath).append("/").toString();
File dirPath = new File(uploadDir);
              if (!dirPath.exists()) {
                  dirPath.mkdirs();
              }
              if (upload == null) {
                  addActionError(getText("maxLengthExceeded"));
                  return "input";
              }else if(upload.length()==0){
                  addActionError(getText("emptyFile"));
              }
              if (upload != null) {

File distFile = new File((uploadDir + this.fileName).toString());
                  try {
                      copyFile(upload, distFile);
                  } catch (Exception e) {

System.out.println(e.getMessage()); } } }
        return SUCCESS;
    }
    //add Uploader
    public void copyFile(File in, File out) throws Exception {
        FileInputStream fis = new FileInputStream(in);
        FileOutputStream fos = new FileOutputStream(out);
        byte[] buf = new byte[1024];
        int i = 0;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fis.close();
        fos.close();

    }
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getItemType() {
        return itemType;
    }

    public void setItemType(String itemType) {
        this.itemType = itemType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
// add Uploader public String getUploadFileName() {
        return fileName;
    }

    public void setUploadFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getUploadContentType() {
        return contentType;
    }

    public void setUploadContentType(String contentType) {
        this.contentType = contentType;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }
}
----------------------action end----------------------

--Kurapica

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to