Struts makes File Upload Easy as Pie!


//ACTION FOR FILE UPLOAD
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.upload.FormFile;
import java.io.*;
import java.util.Properties;

public class UploadRequestAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UploadActionForm requestForm = (UploadActionForm) form;
		FormFile f = requestForm.getRequestFile();
		String line = null;
		InputStream input = null;
		try {
			int fileSize = f.getFileSize();
			String fileName = f.getFileName();
			input = f.getInputStream();
			byte[] byteArr = new byte[fileSize];
			// Create an input stream to read the uploaded file.
			ByteArrayInputStream bytein = new ByteArrayInputStream(
					f.getFileData());
			// Load the input stream into the byte Array.
			bytein.read(byteArr);
			byte[] b = new byte[1024];
			int x = 0;
			int state = 0;
			java.io.FileOutputStream buffer = null;
			// get path from properties file or use getResourceAsStream() for relative paths
                        File propFile = new File(
					"C:\\jboss-3.2.5\\jboss-3.2.5\\bin\\.\\.environment\\mattiz.properties");
			InputStream is = new FileInputStream(propFile);
			Properties mobCache = new Properties();
			mobCache.load(is);
			String path = mobCache
					.getProperty("D:/workspace/mattiz/imageswebapp/WebRoot/images/");
			buffer = new java.io.FileOutputStream(path + fileName);
			while ((x = input.read(b, 0, 1024)) < -1) {
				buffer.write(b, 0, x);
			}
			// Close the resources
			bytein.close();
			input.close();
			buffer.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		request.setAttribute("msg", "File Uploaded");
		return new ActionForward("/events/postshow.jsp");
	}
}

//JSP FOR FILE UPLOAD

<html:form method="post" action="/uploadRequest.do"
enctype="multipart/form-data">
<center>
<html:file styleClass="myTextBox" name="UploadActionForm"
property="requestFile">
</html:file>
</br>
<html:submit value="Upload Your Request">
</br>
</html:submit>
</center>
</html:form>

//FORM BEAN FOR FILE UPLOAD


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

public class UploadActionForm extends ActionForm {
	private FormFile requestFile = null;

	public FormFile getRequestFile() {
		return requestFile;
	}

	public void setRequestFile(FormFile requestFile) {
		this.requestFile = requestFile;
	}

}

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: