1 |
torben |
593 |
package com.grundfos.gmatou.dataupload;
|
2 |
|
|
|
3 |
|
|
import com.oreilly.servlet.multipart.*;
|
4 |
|
|
|
5 |
|
|
import java.sql.*;
|
6 |
|
|
import java.io.*;
|
7 |
|
|
import java.util.*;
|
8 |
|
|
import javax.servlet.ServletException;
|
9 |
|
|
import javax.servlet.http.HttpServlet;
|
10 |
|
|
import javax.servlet.http.HttpServletRequest;
|
11 |
|
|
import javax.servlet.http.HttpServletResponse;
|
12 |
|
|
|
13 |
|
|
/**
|
14 |
|
|
* Servlet implementation class DataReceiver
|
15 |
|
|
*/
|
16 |
|
|
public class DataReceiver extends HttpServlet {
|
17 |
|
|
private static final long serialVersionUID = 1L;
|
18 |
|
|
|
19 |
|
|
/**
|
20 |
|
|
* @see HttpServlet#HttpServlet()
|
21 |
|
|
*/
|
22 |
|
|
public DataReceiver() {
|
23 |
|
|
super();
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
private static final int SIZE = 10000000;
|
27 |
|
|
|
28 |
|
|
private Map<String,String> getParts(HttpServletRequest request) throws ServletException {
|
29 |
|
|
Map<String,String> response = new HashMap<String,String>();
|
30 |
|
|
try {
|
31 |
|
|
MultipartParser parser = new MultipartParser(request, SIZE);
|
32 |
|
|
|
33 |
|
|
Part part = null;
|
34 |
|
|
while ((part = parser.readNextPart()) != null) {
|
35 |
|
|
|
36 |
|
|
if (part.getName().equals("data")) {
|
37 |
|
|
FilePart image = (FilePart) part;
|
38 |
|
|
|
39 |
|
|
InputStream is = image.getInputStream();
|
40 |
|
|
|
41 |
|
|
BufferedReader reader = new BufferedReader(
|
42 |
|
|
new InputStreamReader(is));
|
43 |
|
|
StringBuilder sb = new StringBuilder();
|
44 |
|
|
|
45 |
|
|
String line;
|
46 |
|
|
while ((line = reader.readLine()) != null) {
|
47 |
|
|
sb.append(line + "\n");
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
reader.close();
|
51 |
|
|
is.close();
|
52 |
|
|
|
53 |
|
|
response.put("data", sb.toString());
|
54 |
|
|
} else if (part.getName().equals("device")) {
|
55 |
|
|
ParamPart pp = (ParamPart) part;
|
56 |
|
|
response.put("device", pp.getStringValue());
|
57 |
|
|
}
|
58 |
|
|
}// while
|
59 |
|
|
} catch (Exception e) {
|
60 |
|
|
throw new ServletException(e);
|
61 |
|
|
}
|
62 |
|
|
return response;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
protected void doPost(HttpServletRequest request,
|
66 |
|
|
HttpServletResponse response) throws ServletException, IOException {
|
67 |
|
|
Map<String,String> parts = getParts(request);
|
68 |
|
|
|
69 |
|
|
String device = parts.get("device");
|
70 |
|
|
String data = parts.get("data");
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
if (device == null || data == null)
|
74 |
|
|
throw new ServletException("insufficient data");
|
75 |
|
|
|
76 |
|
|
try {
|
77 |
|
|
Connection conn = DBConnection.getDbConnection();
|
78 |
|
|
PreparedStatement stmt = conn.prepareStatement("INSERT INTO dataentries (date,device,data) VALUES (now(), ?,?)");
|
79 |
|
|
stmt.setString(1, device);
|
80 |
|
|
stmt.setString(2, data);
|
81 |
|
|
stmt.execute();
|
82 |
|
|
|
83 |
|
|
stmt.close();
|
84 |
|
|
conn.close();
|
85 |
|
|
|
86 |
|
|
} catch (Exception e) {
|
87 |
|
|
throw new ServletException(e);
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
}
|