• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using FormFile in struts

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic