1 |
package dk.thoerup.pictureposterservice; |
2 |
|
3 |
import java.io.ByteArrayOutputStream; |
4 |
import java.io.FileOutputStream; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
import java.io.UnsupportedEncodingException; |
8 |
import java.net.URLDecoder; |
9 |
import java.util.Date; |
10 |
import java.util.Map; |
11 |
import java.util.TreeMap; |
12 |
import java.util.logging.Logger; |
13 |
|
14 |
import javax.servlet.ServletException; |
15 |
import javax.servlet.http.HttpServlet; |
16 |
import javax.servlet.http.HttpServletRequest; |
17 |
import javax.servlet.http.HttpServletResponse; |
18 |
|
19 |
import com.oreilly.servlet.multipart.FilePart; |
20 |
import com.oreilly.servlet.multipart.MultipartParser; |
21 |
import com.oreilly.servlet.multipart.ParamPart; |
22 |
import com.oreilly.servlet.multipart.Part; |
23 |
|
24 |
/** |
25 |
* Servlet implementation class PostServlet |
26 |
*/ |
27 |
public class PostServlet extends HttpServlet { |
28 |
private static final long serialVersionUID = 1L; |
29 |
Logger logger = Logger.getLogger(PostServlet.class.getName()); |
30 |
|
31 |
protected Map<String,String> decodeString(String str) throws UnsupportedEncodingException { |
32 |
Map<String,String> map = new TreeMap<String,String>(); |
33 |
|
34 |
String[] pairs = str.split("&"); |
35 |
for (String pair : pairs) { |
36 |
String keyValue[] = pair.split("="); |
37 |
String key = keyValue[0]; |
38 |
|
39 |
String val = ""; |
40 |
if (keyValue.length == 2) { |
41 |
val = URLDecoder.decode(keyValue[1], "UTF-8"); |
42 |
} |
43 |
map.put(key, val); |
44 |
} |
45 |
|
46 |
return map; |
47 |
} |
48 |
|
49 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
50 |
MultipartParser parser = new MultipartParser(request,1000000); |
51 |
|
52 |
Map<String,String> values = null; |
53 |
byte file[] = null; |
54 |
|
55 |
Part part; |
56 |
while ( (part= parser.readNextPart()) != null) { |
57 |
if (part.getName().equals("file") ) { |
58 |
FilePart fpart = (FilePart) part; |
59 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(128000); |
60 |
InputStream is = fpart.getInputStream(); |
61 |
|
62 |
byte data[] = new byte[8192]; |
63 |
int len; |
64 |
while ( (len = is.read(data)) !=-1 ) { |
65 |
baos.write(data, 0, len); |
66 |
} |
67 |
is.close(); |
68 |
file = baos.toByteArray(); |
69 |
baos.close(); |
70 |
|
71 |
} else { |
72 |
ParamPart params = (ParamPart) part; |
73 |
values = decodeString( params.getStringValue() ); |
74 |
} |
75 |
} |
76 |
|
77 |
Posting post = new Posting(); |
78 |
post.setTitle( values.get("title") ); |
79 |
post.setNote( values.get("note") ); |
80 |
post.setAuthor(values.get("author")); |
81 |
if ( !values.get("latitude").equals("") ) { |
82 |
post.setLatitude( Double.parseDouble(values.get("latitude")) ); |
83 |
} |
84 |
if ( !values.get("longitude").equals("") ) { |
85 |
post.setLongitude( Double.parseDouble(values.get("longitude")) ); |
86 |
} |
87 |
|
88 |
post.setPostdate( new Date() ); // now() |
89 |
|
90 |
|
91 |
PostingDAO.savePosting(post); |
92 |
logger.info("Saved with postID=" +post.getId()); |
93 |
|
94 |
String fileName = "/home/app/data/PicturePoster/" + post.getId() + ".jpeg"; |
95 |
|
96 |
FileOutputStream fo = new FileOutputStream(fileName); |
97 |
fo.write(file); |
98 |
fo.close(); |
99 |
} |
100 |
|
101 |
} |