{FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);fileOutStream.write(myFile.getFileData());fileOutStream.flush();fileOutStream.close();}
}//Set file name to the request objectrequest.setAttribute(”fileName”,fileName);return mapping.findForward(”success”);}}
/* This action class first takes the file which is set in the bean. then its actual path is stored in thevariable filepath.then if the filepath is not empty, a new file with same name is created inside realpath of servlet or jsp/uploadif the filepath is empty than fileOutStream is flushed.& in the end name of the file is set in the request scope such that the same file can bedownloaded latter. Now enter the Code in
Struts Config
such that their is right mapping of the code.
<struts-config><form-beans><form-bean name=”FileUpload” type=”beans.StrutsUploadAndSaveForm”/>
</form-beans><action-mappings><action path=”/fileUpload” type=”action.StrutsUploadAndSaveAction”name=”FileUpload” scope=”request” validate=”true” input=”/FileUploadAndSave.jsp”><forward name=”success” path=”/downloadfile.jsp”/></action></action-mappings></struts-config>
Now create a jsp named
downloadfile.jsp<html><head><title>Success</title></head><body><%String fileName=(String)request.getAttribute(”fileName”);%>
Leave a Comment