KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UploadAction


1 import java.io.*;
2 import java.util.*;
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 import jodd.servlet.*;
7 import beans.*;
8 import jodd.bean.*;
9
10 public class UploadAction extends ActionServlet {
11
12     public String doAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
13         System.out.println("---------------------");
14         System.out.println("UploadAction.doAction()");
15         String temp = System.getProperty("java.io.tmpdir");
16
17
18         //System.out.println("==>" + request.getParameter("data3"));
19
//System.out.println("==>" + (request.getParameterValues("data3")).length);
20

21         MultipartRequest mrequest = new MultipartRequest(request, "Windows-1250");
22
23         System.out.println("==>" + mrequest.getParameter("Data3"));
24         String[] v = mrequest.getParameterValues("Data3");
25         if (v != null) {
26             for (int i = 0; i < v.length; i++) {
27                 System.out.println(" >" + v[i]);
28             }
29         }
30
31         System.out.println("PARAMETERS");
32         Enumeration names = mrequest.getParameterNames();
33         while (names.hasMoreElements()) {
34             String paramName = (String) names.nextElement();
35             System.out.println(paramName);
36             System.out.println(mrequest.getParameter(paramName));
37             System.out.println("---");
38         }
39
40         System.out.println("\nFILES");
41         names = mrequest.getUploadedFileNames();
42         while (names.hasMoreElements()) {
43             String paramName = (String) names.nextElement();
44             System.out.println(paramName);
45             UploadedFile uf = mrequest.getUploadedFile(paramName);
46             if (uf.isValid()) {
47                 System.out.println(uf.getFileName());
48                 if (temp != null) {
49                     String localFile = temp + "/" + uf.getFileName();
50                     System.out.println("write to: " + localFile);
51                     uf.writeData(localFile);
52                 }
53             } else {
54                 System.out.println("not valid");
55             }
56             System.out.println("---");
57         }
58
59
60
61         System.out.println("++++++++++++++++++++++++++++++++++++++++++++++");
62
63         UploadFormBean ufb = new UploadFormBean();
64         BeanUtil.load(ufb, mrequest);
65         System.out.println(ufb.getData1());
66         System.out.println(ufb.getData2());
67         System.out.println(ufb.getData3());
68         System.out.println(ufb.getFile1());
69         System.out.println(ufb.getFile2());
70         System.out.println(ufb.getFile3());
71         System.out.println(ufb.getFile4());
72
73         request.setAttribute("UploadFormBean", ufb);
74
75         return "upload2.jsp";
76     }
77
78 }
79
80
Popular Tags