KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > remote > FileUploadServlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.deployment.remote;
18
19 import javax.servlet.http.HttpServlet JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.DataInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.BufferedOutputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29
30 /**
31  * A servlet that accepts file uploads. It takes only POST requests, which should
32  * contain a Java "DataOutput" formatted stream containing:
33  *
34  * 1) an int, the number of files being uploaded
35  * 2) for each file:
36  * 1) an int, the length of the file in bytes
37  * 2) a number of raw bytes equal to the above for the file
38  *
39  * It returns a serialized stream containing:
40  *
41  * 1) a UTF string, the status (should be "OK")
42  * 2) an int, the number of files received
43  * 3) for each file:
44  * 1) a UTF String, the path to the file as saved to the server's filesystem
45  *
46  * The file positions in the response will be the same as in the request.
47  * The is, a name for upload file #2 will be in response position #2.
48  *
49  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
50  */

51 public class FileUploadServlet extends HttpServlet JavaDoc {
52     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
53         int fileCount;
54         String JavaDoc names[];
55         try {
56             DataInputStream JavaDoc in = new DataInputStream JavaDoc(request.getInputStream());
57             fileCount = in.readInt();
58             names = new String JavaDoc[fileCount];
59             for(int i=0; i<fileCount; i++) {
60                 int length = in.readInt();
61                 File JavaDoc temp = File.createTempFile("remote-deploy", "");
62                 temp.deleteOnExit();
63                 names[i] = temp.getAbsolutePath();
64                 readToFile(in, temp, length);
65             }
66             in.close();
67         } catch (IOException JavaDoc e) {
68             DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(response.getOutputStream());
69             out.writeUTF("ERROR: "+e.getMessage());
70             out.close();
71             return;
72         }
73         DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(response.getOutputStream());
74         out.writeUTF("OK");
75         out.writeInt(fileCount);
76         for (int i = 0; i < names.length; i++) {
77             out.writeUTF(names[i]);
78         }
79         out.flush();
80         out.close();
81     }
82
83     private static void readToFile(DataInputStream JavaDoc in, File JavaDoc temp, int length) throws IOException JavaDoc {
84         BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(temp));
85         int total, read;
86         try {
87             byte[] buf = new byte[1024];
88             total = 0;
89             while((read = in.read(buf, 0, Math.min(buf.length, length - total))) > -1) {
90                 out.write(buf, 0, read);
91                 total += read;
92                 if(total == length) {
93                     break;
94                 }
95             }
96         } finally {
97             try {out.flush();} catch (IOException JavaDoc e) {}
98             out.close();
99         }
100         if(total != length) {
101             throw new IOException JavaDoc("Unable to read entire upload file ("+total+"b expecting "+length+"b)");
102         }
103     }
104 }
105
106
Popular Tags