KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > applications > webtalk > controller > UploadFileAction


1 /*
2  * UploadFileAction.java
3  *
4  * Created on June 8, 2003, 8:06 PM
5  */

6
7 package com.quikj.application.communicator.applications.webtalk.controller;
8
9 import java.io.*;
10 import java.util.*;
11 import java.sql.*;
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14 import org.apache.struts.action.*;
15 import com.quikj.application.communicator.admin.model.*;
16 import com.quikj.application.communicator.admin.controller.*;
17 import com.quikj.server.framework.*;
18
19
20 /**
21  *
22  * @author Vinod Batra
23  */

24 public final class UploadFileAction extends Action
25 {
26     
27     public static final String JavaDoc APP_PATH="aceapp";
28     public static final String JavaDoc DATA_PATH="data";
29     public static final String JavaDoc HTML_PATH="html";
30     
31     /** Creates a new instance of UploadFileAction */
32     public UploadFileAction()
33     {
34     }
35     
36     public ActionForward execute(ActionMapping mapping,
37     ActionForm form,
38     HttpServletRequest request,
39     HttpServletResponse response)
40     throws IOException, ServletException
41     {
42         // Extract attributes we will need
43
Locale locale = getLocale(request);
44         ActionErrors errors = new ActionErrors();
45         
46         // first check if the user has a connection
47
Connection c = (Connection)request.getSession().getAttribute("connection");
48         if (c == null)
49         {
50             errors.add(ActionErrors.GLOBAL_ERROR,
51             new ActionError("error.not.logged.in"));
52             saveErrors(request, errors);
53             return mapping.findForward("logon");
54         }
55         
56         AccountElement element = (AccountElement)request.getSession().getAttribute("userInfo");
57         if ((element.isAdminLevel() == false) && (element.isCustomerLevel() == false))
58         {
59             errors.add(ActionErrors.GLOBAL_ERROR,
60             new ActionError("error.insufficient.privilege"));
61             saveErrors(request, errors);
62             
63             return mapping.findForward("main_menu");
64         }
65         
66         UploadFileForm uploadform = (UploadFileForm) form;
67         
68         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(request.getSession().getServletContext().getRealPath(""));
69         
70         buffer.append(File.separator);
71         buffer.append(APP_PATH);
72         
73         buffer.append(File.separator);
74         buffer.append(DATA_PATH);
75         
76         buffer.append(File.separator);
77         buffer.append(HTML_PATH);
78         
79         buffer.append(File.separator);
80         buffer.append(element.getDomain());
81         
82         File out_directory = new File(buffer.toString());
83         
84         if (!out_directory.exists() || !out_directory.isDirectory())
85         {
86             out_directory.mkdirs();
87         }
88         
89         File out_file = new File(out_directory, uploadform.getFileName());
90         
91         if (out_file.createNewFile() == false)
92         {
93             errors.add("userFile", new ActionError("file.already.exists"));
94             saveErrors(request, errors);
95             return (new ActionForward(mapping.getInput()));
96         }
97         
98         try
99         {
100             FileOutputStream out = new FileOutputStream(out_file);
101             out.write(uploadform.getFileBytes());
102         }
103         catch (Exception JavaDoc ex)
104         {
105             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
106             "UploadFileAction.execute() Could not write to file "
107             + out_file.getAbsoluteFile()
108             + " : "
109             + ex.getClass().getName()
110             + " : "
111             + ex.getMessage());
112             
113             out_file.delete();
114             
115             errors.add("userFile", new ActionError("file.write.error"));
116             saveErrors(request, errors);
117             return (new ActionForward(mapping.getInput()));
118         }
119         
120         String JavaDoc fileSuccess = "uploading";
121         request.getSession().setAttribute("fileSuccess", fileSuccess);
122         
123         WebTalkRelatedTasks menu = new WebTalkRelatedTasks();
124         menu.addLink(new LinkAttribute("Delete File", "delete_file_input"));
125         menu.addLink(new LinkAttribute("Rename File", "rename_file_input"));
126         return (mapping.findForward("file_success"));
127         
128     }
129 }
Popular Tags