This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Using FormFile in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using FormFile in struts" Watch "Using FormFile in struts" New topic
Author

Using FormFile in struts

Divya Prasad
Greenhorn

Joined: Feb 28, 2006
Posts: 1
I have given my jsp as <%@page language="java"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

<html:html>
<html:form enctype="multipart/form-data" action="/action/LoginAction">

<h1>Add Article</h1>
Please enter file you wish to add:
<br><br>
<html:file property="uploadedFile"/>
<br><br>
<html:submit value="Upload File"/>
</html:form>

</html:html>


FormBean as package com.mot.cdb.forms;

import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;

public class UploadFileForm extends ActionForm {
FormFile uploadedFile = null;

public void setUploadedFile(FormFile file) {

uploadedFile = file;
System.out.println("uploadedFile"+uploadedFile);
} // setUploadedFile

public FormFile getUploadedFile() {
return uploadedFile;
} // getUploadedFile
} // UploadedFileForm
and FormAction as
package com.mot.cdb.actions;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

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

import com.mot.cdb.forms.UploadFileForm;

public class UploadFileAction {

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
System.out.println("1");
UploadFileForm fileForm = (UploadFileForm)form;
System.out.println("2");
FormFile uploadedFile = fileForm.getUploadedFile();
System.out.println("3");
InputStream uploadInStream = uploadedFile.getInputStream();
System.out.println("4");
String filename = uploadedFile.getFileName();
System.out.println("5");
FileOutputStream fOut = new FileOutputStream("c:\\uploads\\"+filename);
System.out.println("6");
int c=0;
while ((c=uploadInStream.read()) != -1) {
System.out.println("7");
fOut.write(c);
} // while
fOut.flush();
System.out.println("8");
fOut.close();
System.out.println("9");
String forwardPath = "Success";
System.out.println("10");
// perform the rest of the action here
return mapping.findForward(forwardPath);
}



}
and in strutsconfig.xml <form-beans>
<form-bean name="uploadForm" type="com.mot.cdb.forms.UploadFileForm"/>
</form-beans>
<action path="/action/upload" name="uploadForm" scope="request" type="com.mot.cdb.actions.UploadFileAction">
<forward name="Success" path="UploadJSP"/>
</action>


I got error as javax.servlet.jsp.JspException: No getter method for property uploadedFile of bean org.apache.struts.taglib.html.BEAN




Can anyone help me in this .Thanks in advance.
 
Don't get me started about those stupid light bulbs.
 
subject: Using FormFile in struts
 
Similar Threads
struts file upload problem
How do I get request parameters when the form is encoded?
Upload file using Struts
Exception: related to struts ----need help
Exception: related to struts -need help