1 | /* |
---|
2 | * Created on 31 mars 2005 |
---|
3 | * |
---|
4 | * TODO To change the template for this generated file go to |
---|
5 | * Window - Preferences - Java - Code Style - Code Templates |
---|
6 | */ |
---|
7 | package com.medias.megapoli.struts.actions; |
---|
8 | |
---|
9 | import java.io.IOException; |
---|
10 | |
---|
11 | import javax.servlet.ServletException; |
---|
12 | import javax.servlet.http.HttpServletRequest; |
---|
13 | import javax.servlet.http.HttpServletResponse; |
---|
14 | import java.io.*; |
---|
15 | import org.apache.struts.action.Action; |
---|
16 | import org.apache.struts.action.ActionError; |
---|
17 | import org.apache.struts.action.ActionErrors; |
---|
18 | import org.apache.struts.action.ActionForm; |
---|
19 | import org.apache.struts.action.ActionForward; |
---|
20 | import org.apache.struts.action.ActionMapping; |
---|
21 | import com.medias.Context; |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * @author pignot |
---|
26 | * |
---|
27 | * TODO To change the template for this generated type comment go to |
---|
28 | * Window - Preferences - Java - Code Style - Code Templates |
---|
29 | */ |
---|
30 | public class SendFileToClientAction extends Action { |
---|
31 | |
---|
32 | public ActionForward execute (ActionMapping mapping, |
---|
33 | ActionForm form, |
---|
34 | HttpServletRequest request, |
---|
35 | HttpServletResponse response) |
---|
36 | throws IOException, ServletException |
---|
37 | { |
---|
38 | ActionErrors errors = new ActionErrors(); |
---|
39 | // String filename = (String)request.getParameter ("fileName"); |
---|
40 | String filePath = (String)request.getParameter ("path"); |
---|
41 | |
---|
42 | response.setContentType ("multipart/zip"); |
---|
43 | |
---|
44 | // if (filename == null && filePath == null) { |
---|
45 | // filename = (String)request.getSession ().getAttribute ("fileName"); |
---|
46 | // filePath = (String)request.getSession ().getAttribute ("filePath"); |
---|
47 | // } |
---|
48 | |
---|
49 | // String filename = (String)request.getParameter ("fileName"); |
---|
50 | // String filePath = (String)request.getParameter ("filePath"); |
---|
51 | |
---|
52 | // System.out.println ("######### SENDFILETOCLIENT FileName = " + filename); |
---|
53 | // System.out.println ("######### SENDFILETOCLIENT FilePath = " + filePath); |
---|
54 | |
---|
55 | String fileName = filePath.substring(filePath.lastIndexOf("/")+1); |
---|
56 | String uploadDir = (String)request.getSession().getServletContext().getAttribute("uploadDir"); |
---|
57 | String accessDir = (String)request.getSession().getServletContext().getAttribute("accessDir"); |
---|
58 | |
---|
59 | response.setHeader ("Content-Disposition","attachment; filename=\"" +fileName.trim () + "\";"); |
---|
60 | |
---|
61 | File f = new File (accessDir+"/"+filePath); |
---|
62 | if (!f.exists()) { |
---|
63 | f = new File (uploadDir+"/"+filePath); |
---|
64 | } |
---|
65 | |
---|
66 | response.setContentLength((int)f.length()); |
---|
67 | |
---|
68 | try { |
---|
69 | OutputStream os = response.getOutputStream (); |
---|
70 | FileInputStream stream = new FileInputStream (f); |
---|
71 | |
---|
72 | BufferedInputStream bis = new BufferedInputStream (stream); |
---|
73 | InputStream is = new BufferedInputStream (bis); |
---|
74 | |
---|
75 | int count; |
---|
76 | byte buf[] = new byte[4096]; |
---|
77 | while ((count = is.read (buf)) > -1) { |
---|
78 | os.write (buf, 0, count); |
---|
79 | } |
---|
80 | is.close (); |
---|
81 | os.close (); |
---|
82 | } |
---|
83 | catch (Exception ex) |
---|
84 | { |
---|
85 | System.out.println ("exception_sendFileToClient"); |
---|
86 | errors.add (ActionErrors.GLOBAL_MESSAGE, new ActionError ("errors.access.send.failed",Context.getWebmaster(request))); |
---|
87 | ex.printStackTrace (); |
---|
88 | } |
---|
89 | |
---|
90 | if(!errors.isEmpty ()){ |
---|
91 | System.out.println ("errors"); |
---|
92 | saveErrors(request, errors); |
---|
93 | } |
---|
94 | |
---|
95 | return null; |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|