View Javadoc

1   /*
2    * 
3    * 
4    */
5   package example.struts;
6   
7   import org.apache.struts.action.Action;
8   import org.apache.struts.action.ActionMapping;
9   import org.apache.struts.action.ActionForm;
10  import org.apache.struts.action.ActionForward;
11  import org.apache.struts.upload.FormFile;
12  
13  import example.filestorage.*;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.io.InputStream;
18  
19  /***
20   * 
21   * @author Sean C. Sullivan
22   *
23   * 
24   * 
25   */
26  public class FileUploadAction extends Action
27  {
28  	public ActionForward execute(ActionMapping mapping,
29              ActionForm form,
30              HttpServletRequest request,
31              HttpServletResponse response) throws Exception 
32  	{
33  		FileUploadForm uploadForm = (FileUploadForm) form;
34  		
35  		FormFile file = uploadForm.getFile();
36  
37  		ActionForward forward = null;
38  
39  		InputStream input = null;
40  		
41  		try
42  		{
43  			int fileSize = file.getFileSize();
44  			String fileName = file.getFileName();
45  			
46  			input = file.getInputStream();
47  
48  			FileStorageDAO dao = DAOFactory.getFileStorageDAO();
49  			
50  			dao.saveFile(fileName, input, fileSize);
51  			
52  			forward = mapping.findForward(Forward.KEY_SUCCESS);
53  		}
54  		finally
55  		{
56  			if (input != null)
57  			{
58  				input.close();
59  			}
60  			file.destroy();
61  		}
62  		
63  		return forward;
64  	}
65  
66  }