wood burning stoves 2.0*
The moose likes Struts and the fly likes File upload with websphere 6. file object is stripped. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "File upload with websphere 6. file object is stripped. " Watch "File upload with websphere 6. file object is stripped. " New topic
Author

File upload with websphere 6. file object is stripped.

Erdal Guner
Greenhorn

Joined: Apr 10, 2008
Posts: 7
Hi All,

I have been looking into the examples for uploading files using struts and tried many ways to make it work on RAD 6.0 dev environment (WebSphere 6). For some reason the type="file" form field is not getting posted to the Action. I am using working examples from other posters (thanks). Still cannot figure out why the file is being stripped out.

I have added new entries into my struts-config-ext.xml (Stores/WebContent/WEB-INF):

...
<form-beans>
<form-bean name="myForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="myFile" type="org.apache.struts.upload.FormFile"/>
</form-bean>
</form-beans>
...

Note that I am using DynaActionForm.

...

Here is the Action I defined:
<action
path="/FileUpload"
type="com.nsp.utils.StrutsUploadAction"
name="myForm"
scope="request"
validate="true"
input="/NSP/nsp_nspfileupload.jsp">
<forward name="success" path="/NSP/nsp_nspfileupload.jsp"/>
</action>

...

The Action I use is here. The issue as well. myFile comes as null all the time.

package com.nsp.utils;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;

/**
*
*/
public class StrutsUploadAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
DynaActionForm myForm = (DynaActionForm)form;

if (myForm!=null) {
// Process the FormFile
FormFile myFile = (FormFile)myForm.get("myFile");
// !!! myFile is always null!!!
if (myFile!=null) {
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();

System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName);
System.out.println("File Size: " + fileSize);
}
}
return mapping.findForward("success");
}
}


And here is the JSP I am using to get the file:

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
<head>
<title>File Upload</title>
<html:base />
</head>
<body bgcolor="white">
<html:form action="FileUpload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="left" colspan="2">
<font color="red"><html:errors /></font>
</tr>


<tr>
<td align="right">
File Name
</td>
<td align="left">
<html:file property="myFile" />
</td>
</tr>


<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>

</html:form>
</body>
</html:html>

Only Name Value Pair I am getting is the "submit" button.

What am I missing here?

Any help would be greatly appreciated.


Regards,
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I vaguely remember seeing a strange problem similar to yours where the fact that the method="post" attribute was before the enctype attribute made a difference. I'd suggest you remove method="post" entirely since post is the default method for an <html:struts> tag.

Also, check the size of the file. If it's larger than the maxiumum file size specified in the <controller> element of your struts-config.xml file, Stuts will simply return null to the FormFile without any error message.
[ April 10, 2008: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Erdal Guner
Greenhorn

Joined: Apr 10, 2008
Posts: 7
Thanks Merril.

I implemented your suggestion. Removed the method="post". Tried again. Same result. myFile was null.

I do not have any <controller> section in my struts. I think is defaulted tp 250M. I will put the <controller> into struts-config-ext.xml tomorrow to see if that would make any difference.

I also verified that my struts-fileupload.jar is dated 2003. I think it is 1.0 version. I am not sure if there is an incompatibility issue. Also RAD (WebSphere Development env for 6) uses its own struts libraries. I am not aware of any issues there. I looked into the IBM developerworks postings as well.

Thanks again. Any more ideas?
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Have you tried this without using DynaActionForm?
Erdal Guner
Greenhorn

Joined: Apr 10, 2008
Posts: 7
Thanks Anirvan,

I first started with working examples many refer to. Not using DynaActionForm. Same result.

I tried adding other fields (other than submit). The form gets processed but none of the "file" fields get thru. Only regular/hidden/submit fields I can see. myFile in this example was always null.

I might be running out of options as I need to find a solution fast (isn't it always the case?).
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Originally posted by Erdal Guner:

I might be running out of options as I need to find a solution fast (isn't it always the case?).


Hehe, precisely! Anyways, your problem certainly seems to have that unique touch to it, which for a programmer can get very frustrating.

Since we're at it, how about setting validate="false" and removing the "input" attribute from your <action> mapping tag. Just a gut feeling I fear. Can't really vouch, but then we're low on alternatives already!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's one other suggestion: Try downloading Struts 1.1 again from the Struts home page. Then put the upload jar file from that download into your WEB-INF/lib folder and see if that makes a difference.
Erdal Guner
Greenhorn

Joined: Apr 10, 2008
Posts: 7
Thanks all very much.

I implemented both suggestions (put the struts 1.1 into WC/lib and struts-config changes). Unfortunately it didn't work. I added a text field and checked them on debug session. elementsText is full from the CommonsMultipartRequestHandler shows. elementsFile has no entry!

Anything else you can think of? The code is exactly as it is in my original posting (with some suggestion changes in the postings). I just can't make this work.
Erdal Guner
Greenhorn

Joined: Apr 10, 2008
Posts: 7
One comment I found on struts and on some postings was that struts already parses the multipart form, hence removing the form all together might work. I will try and let you know as soon as I get a chance.
Erdal Guner
Greenhorn

Joined: Apr 10, 2008
Posts: 7
I tried everything I could think of...

I keep getting the file object null...
Mike Zingman
Greenhorn

Joined: Sep 10, 2008
Posts: 1
Have the same problem. I think the problem with file upload is in websphere 6 configuration. I would apply the latest cumulative fixpack (17) first, and see what's happening.
Erdal Guner
Greenhorn

Joined: Apr 10, 2008
Posts: 7
Well. I have found the solution. But I have forgotten to post it here.

The issue was never there. WebSphere 6 thru commerce is already handling the upload within its internal code (thru Struts of course). All you need to do is to use com.ibm.commerce.server.UploadedFile.

UploadedFile uFile = (UploadedFile)getRequestProperties().get(ECConstants.EC_UPLOADED_FILE, null);
File file=new File("<directoryHierarchyAndFileName>");
uFile.readAndSaveFile(file);

This will save the file! Remember that uploading is already handled out of the box is you are using commerce. Not sure if you use straight WebSphere it would cover that too. I would assume it maynot. The the regular struts methods should suffice.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: File upload with websphere 6. file object is stripped.