user1917911
user1917911

Reputation: 1

File upload in struts2 using s:file

I am trying to upload the file using S:file tag but it is giving me http 500 internal error. But When I directly give filename and path as constant in same code it works. So I am not sure where I am missing the loop hole.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>

<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror  />
<s:form action="uploadFile.action"  method="post" enctype="multipart/form-data" >
<s:textfield name="myName"></s:textfield>
  <s:file name="myFile"></s:file>
    <s:submit  value="Submit" align="center" />
</s:form>
</body>
</html>

Action

class FileUploadAction extends ActionSupport implements ServletRequestAware {
    File userImage;
    String userImageContentType;
    String userImageFileName;
    HttpServletRequest servletRequest;

    public String execute() {
        try {
            String filePath = servletRequest.getSession()
                    .getServletContext().getRealPath("/");
            File fileToCreate = new File(filePath, this.userImageFileName);
            FileUtils.copyFile(this.userImage, fileToCreate);
        } catch (Exception e) {
            addActionError(e.getMessage());
            return INPUT;
        }
        return SUCCESS;
    }

    public void setServletRequest(HttpServletRequest servletRequest) {
        servletRequest = servletRequest;
    }
}

Config

<action name="userImage" class="net.viralpatel.struts2.FileUploadAction"> 
    <interceptor-ref name="fileUpload"> 
        <param name="maximumSize">2097152</param> 
        <param name="allowedTypes">
                  image/png,image/gif,image/jpeg,image/pjpeg
            </param> 
    </interceptor-ref> 
    <interceptor-ref name="defaultStack">
    </interceptor-ref> 
    <result name="success">SuccessUserImage.jsp</result> 
    <result name="input">UserImage.jsp</result> 
</action>

EDIT: next time edit your post instead of posting code in comments...

Upvotes: 0

Views: 8247

Answers (2)

Aleksandr M
Aleksandr M

Reputation: 24396

You are putting fileUpload interceptor twice to your action interceptor stack because the defaultStack already includes fileUpload interceptor. You can configure interceptors inside stack like that:

<action ...>
  <interceptor-ref name="defaultStack">
    <param name="fileUpload.allowedTypes">
      image/png,image/gif,image/jpeg,image/pjpeg
    </param>
  </interceptor-ref>
  <result ... />
</action>

Also make sure you have public getters/setters for your variables inside action class.

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50203

EDIT: your Action does not have a private File myFile; with its GETTER.

Start correcting that...


The problem is probably in your Action code, not here.

What do you mean that it works if you pass Strings containing file name / path ?

Do you have a private File myFile; with public accessors (getters and setters) in the Action (and File is java.io.File, not some other File) ?

Take a look at this two simple tutorials too:

http://viralpatel.net/blogs/struts-2-file-upload-save-tutorial-with-example/

http://www.roseindia.net/struts/struts2/struts-2-file-upload.shtml

Upvotes: 2

Related Questions