View Javadoc

1   /*
2    * 
3    * 
4    */
5   package example.struts;
6   
7   import org.apache.struts.action.*;
8   
9   import example.filestorage.*;
10  
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import javax.servlet.ServletOutputStream;
14  
15  /***
16   * 
17   * @author Sean C. Sullivan
18   *
19   * 
20   * 
21   */
22  public class FileDownloadAction extends Action
23  {
24  	static private final int MAX_AGE_IN_SECONDS = 600;
25  	
26  	public ActionForward execute(ActionMapping mapping,
27              ActionForm form,
28              HttpServletRequest request,
29              HttpServletResponse response) throws Exception 
30  	{
31  		FileDownloadForm downloadForm = (FileDownloadForm) form;
32  		long id = Long.parseLong(downloadForm.getFileId());
33  
34  		FileStorageDAO dao = DAOFactory.getFileStorageDAO();
35  		FileInfo info = dao.getFileInfo(id);
36  		
37  		response.setContentLength( (int) info.getFileSize());
38  		response.setContentType("application/x-file-download");
39  		response.setHeader("Content-disposition", 
40  						   "attachment; filename="
41  							+ info.getFilename());
42  		response.setHeader("Cache-Control", 
43  		   				   "max-age=" + MAX_AGE_IN_SECONDS);
44  
45  		ServletOutputStream outStream = response.getOutputStream();
46  		
47  		dao.getFileData(id, outStream);
48  
49  		outStream.flush();
50  		
51  		return null;
52  	}
53  
54  }