Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to upload the files using struts

807591Jun 17 2008 — edited Jun 17 2008
Hi all
My requierment is to upload the files from the client machine and save it on the server, the file size must not increase more than 250 MB and to validate that we cannot do validation on the client machine so we have to validate it on the server side. below is my code kindly go through it if there is any problem in Bussiness Logic
1>Upload.jsp
2>UploadForm.java
3>UploadAction.java

*1>Upload.jsp*
<%@ page language="java"%>
<%@ page import="java.io.*"%>

<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>

<html>

<body bgcolor="white">
<form name="uploadForm" action="TestApplication/upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>

<tr>
<td align="left" colspan="2">
<font color="red"></font>
</tr>



<tr>
<td align="right">
File Name
</td>
<td align="left">
<input type="file"  name="theFile"> 
</td>
</tr>


<tr>
<td align="center" colspan="2">
<input type="submit" value="UPLOAD">
</td>
</tr>
</table>


</form>
</body>
</html>
*2>UploadForm.java*
package com.MyPack.Datamatics;

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

public class UploadForm extends ActionForm
{
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private FormFile theFile;
   public UploadForm(){}
  /**
   * @return Returns the theFile.
   */
  public FormFile getThefile() {
    return theFile;
  }
  /**
   * @param theFile The FormFile to set.
   */
  public void setThefile(FormFile theFile) {
    this.theFile = theFile;
  }
} 
*3>UploadAction.java*
package com.MyPack.Datamatics;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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;

public class UploadAction extends Action
{
  public ActionForward execute(
    ActionMapping am,
    ActionForm af,
    HttpServletRequest req,
    HttpServletResponse res) throws Exception{

	UploadForm myForm = (UploadForm)af;
        // Process the FormFile
        FormFile myFile = myForm.getThefile();
        String contentType = myFile.getContentType();
        String fileName    = myFile.getFileName();
		int fileSize       = myFile.getFileSize();
		String data="";
		try{
        java.io.InputStream fileData  = myFile.getInputStream();
	    String path="E:/store";
		File f = new File(path);
		java.io.OutputStream outputStream = new FileOutputStream(f+"/"+fileName);
		int temp = 0;
		byte[] buffer = new byte[8192];
		temp = fileData.read(buffer, 0, 8192);
		while ((temp) != -1) {
		outputStream.write(buffer, 0, temp);
		}
			outputStream.close();
			data = "file has beeb written to E:/store" + fileName;
			fileData.close();

		}
		catch (FileNotFoundException fnf)
		{ System.out.println("Cannot Found the file");
	     }
		 System.out.println(data);
		System.out.println("contentType: " + contentType);
	    System.out.println("File Name: " + fileName);
	    System.out.println("File Size: " + fileSize);
    
      return am.findForward("success");
  }
}
Thanks in Advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 15 2008
Added on Jun 17 2008
2 comments
250 views