KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > action > resource > UploadResourceAction


1 package org.nextime.ion.backoffice.action.resource;
2
3
4 import java.io.File JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13 import org.nextime.ion.backoffice.action.BaseAction;
14 import org.nextime.ion.backoffice.bean.ResourceXmlBean;
15 import org.nextime.ion.backoffice.bean.Resources;
16 import org.nextime.ion.backoffice.form.UploadResourceForm;
17 import org.nextime.ion.common.ResourceServlet;
18
19
20
21 public class UploadResourceAction extends BaseAction {
22
23     public ActionForward perform(ActionMapping mapping,
24                                  ActionForm form,
25                                  HttpServletRequest JavaDoc request,
26                                  HttpServletResponse JavaDoc response)
27         throws IOException JavaDoc, ServletException JavaDoc {
28             
29         // check if user is correctly logged
30
checkUser(request);
31         
32         // get the form
33
UploadResourceForm sform = (UploadResourceForm)form;
34         
35         // retrieve resources selected
36
String JavaDoc id = request.getSession().getAttribute("selectedResources")+"";
37         String JavaDoc path = null;
38         try {
39             ResourceXmlBean bean = Resources.getResourceXmlBean(servlet,id);
40             path = bean.getDirectory();
41         } catch( Exception JavaDoc e ) {
42             throw new ServletException JavaDoc(e);
43         }
44         String JavaDoc realPath = servlet.getServletContext().getRealPath("/");
45         File JavaDoc resources = new File JavaDoc(realPath,ResourceServlet.relativePath);
46         File JavaDoc tresources = new File JavaDoc(resources,path);
47         
48         String JavaDoc on = sform.getFile().getFileName();
49         String JavaDoc ext = (on.indexOf(".")!=-1)?on.substring(on.indexOf(".")+1):"res";
50         String JavaDoc fileName = sform.getName()+"."+ext;
51         
52         File JavaDoc ofile = new File JavaDoc( tresources, fileName );
53         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(ofile);
54         fos.write(sform.getFile().getFileData());
55         
56         // clean the form
57
sform.setName("");
58         
59         // Forward to the next page
60
return (mapping.findForward("view"));
61
62     }
63
64 }
65
Popular Tags