Struts simplify the file uploading process really easy. You dont need third party stuff; just a few lines of code and a couple of tricks will do the job :)
I have included a sample project for you to download and experiment with.
Key parts are :
1. Your form that will include the <html:file> tag, must be configured to handle multipart/form-data and to POST the data to the server, eg :
<html:form enctype="multipart/form-data" method="POST" action="upload.do">
2. The form bean that will handle the uploading, must have a property for the file been uploaded set like this :
<form-property name="filename" type="org.apache.struts.upload.FormFile">
After that, coding is really simple. I have included a complete project for uploads at the end of this post. Here follows the java source part for the upload file process:
package fileuploader.view; import java.io.File; import java.io.FileOutputStream; import java.io.IOException;
import java.text.DecimalFormat;
import javax.servlet.ServletException; 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 UploadAction extends Action { /** * This is the main action called from the Struts framework. * @param mapping The ActionMapping used to select this instance. * @param form The optional ActionForm bean for this request. * @param request The HTTP Request we are processing. * @param response The HTTP Response we are processing. * @throws javax.servlet.ServletException * @throws java.io.IOException * @return */
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, Exception { String toPage = ""; DynaActionForm df = null; FormFile webFile = null; FileOutputStream ServerFileStream = null; File SaveToFile = null; int FILE_SIZE_LIMIT = 5000000; double filesize = 0 ;
try { //convert to a DynaActionForm df = (DynaActionForm) form; //class cast to a struts type FormFile filename webFile = (FormFile) df.get("filename");
//get the size of the file that user wants to upload filesize = webFile.getFileSize();
//i have included a file size limitation in this version if (filesize > FILE_SIZE_LIMIT ) { //format the size of the file to return two decimals String pattern = "####.##"; DecimalFormat dblFormat = new DecimalFormat(pattern); //file size returned to megabytes String strFileSize = dblFormat.format((filesize / 1024) / 1024);
//redirect to the page that informs the user that there is a //file size limitation toPage = "filesizepage"; request.setAttribute("fsize", strFileSize); return mapping.findForward(toPage); } //get the real path of the project String strFilePath = getServlet().getServletContext().getRealPath("/"); //we want to upload to UploadedFiles directory strFilePath = strFilePath + "UploadedFiles";
//initialize a file to write to SaveToFile = new File(strFilePath, webFile.getFileName());
//in this version, if the file to upload already exists on the server //i delete the server-side one and reupload it if(SaveToFile.exists()) { //delete the file SaveToFile.delete(); } //save the file to server ServerFileStream = new FileOutputStream(SaveToFile); ServerFileStream.write(webFile.getFileData()); ServerFileStream.flush(); ServerFileStream.close(); ServerFileStream = null;
toPage="success"; } catch(Exception e) { //just print the trace e.printStackTrace(); //and redirect to an error page toPage = "error"; } finally { //release resources df = null; webFile = null; ServerFileStream = null; SaveToFile = null; }
return mapping.findForward(toPage);
} }
Download the full jdeveloper sample project (right-click and select "save target as")
just remember to rename it to .rar |
Thanks for the source man