Skip to content

Commit 5d56047

Browse files
Test Servlet for Download, Upload from Android App
1 parent 0e56a5f commit 5d56047

File tree

10 files changed

+407
-0
lines changed

10 files changed

+407
-0
lines changed

misc/TestAndroid/.project

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>TestAndroid</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.wst.validation.validationbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
31+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
32+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
33+
<nature>org.eclipse.jdt.core.javanature</nature>
34+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
35+
</natures>
36+
</projectDescription>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
3+
<display-name>TestAndroid</display-name>
4+
<welcome-file-list>
5+
<welcome-file>index.html</welcome-file>
6+
<welcome-file>index.htm</welcome-file>
7+
<welcome-file>index.jsp</welcome-file>
8+
<welcome-file>default.html</welcome-file>
9+
<welcome-file>default.htm</welcome-file>
10+
<welcome-file>default.jsp</welcome-file>
11+
</welcome-file-list>
12+
</web-app>
30.7 KB
Loading

misc/TestAndroid/WebContent/test.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5+
<title>Insert title here</title>
6+
</head>
7+
<body>
8+
<form method="POST" action="/TestAndroid/TestServlet">
9+
Name: <input type="text" name="text" />
10+
<br/>
11+
<input type="submit" name="send" />
12+
</form>
13+
</body>
14+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5+
<title>Insert title here</title>
6+
</head>
7+
<body>
8+
<form method="POST" action="/TestAndroid/UploadServlet" enctype="multipart/form-data">
9+
Param: <input type="text" name="param1" />
10+
<br/>
11+
Param1: <input type="text" name="param2" />
12+
<br/>
13+
14+
File: <input type="file" name="file" />
15+
<br/>
16+
<input type="submit" name="send" />
17+
</form>
18+
</body>
19+
</html>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2014 Francesco Azzola
3+
* Surviving with Android (http://www.survivingwithandroid.com)
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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+
18+
package com.survivingwithandroid.jee.http;
19+
20+
import java.io.File;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.util.Collection;
25+
26+
import javax.servlet.ServletException;
27+
import javax.servlet.annotation.MultipartConfig;
28+
import javax.servlet.annotation.WebServlet;
29+
import javax.servlet.http.Cookie;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
33+
import javax.servlet.http.Part;
34+
35+
/**
36+
* Servlet implementation classCookieServlet
37+
*/
38+
@WebServlet("/CookieServlet")
39+
@MultipartConfig
40+
public class CookieServlet extends HttpServlet {
41+
private static final long serialVersionUID = 1L;
42+
43+
44+
45+
/**
46+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
47+
*/
48+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49+
cookie(request,response);
50+
}
51+
52+
/**
53+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
54+
*/
55+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
56+
cookie(request,response);
57+
}
58+
59+
private void cookie(HttpServletRequest request, HttpServletResponse response) throws ServletException {
60+
61+
Cookie[] cList = request.getCookies();
62+
63+
if (cList == null) {
64+
Cookie c = new Cookie("swa", "great");
65+
response.addCookie(c);
66+
response.setStatus(HttpServletResponse.SC_OK);
67+
return ;
68+
}
69+
70+
for (Cookie c : cList)
71+
System.out.println("Name ["+c.getName()+"] - Val ["+c.getValue()+"]");
72+
73+
response.setStatus(HttpServletResponse.SC_OK);
74+
}
75+
76+
77+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2014 Francesco Azzola
3+
* Surviving with Android (http://www.survivingwithandroid.com)
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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+
18+
package com.survivingwithandroid.jee.http;
19+
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.IOException;
23+
import java.io.OutputStream;
24+
25+
import javax.servlet.ServletException;
26+
import javax.servlet.annotation.WebServlet;
27+
import javax.servlet.http.HttpServlet;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
31+
32+
/**
33+
* Servlet implementation class DownloadServlet
34+
*/
35+
@WebServlet("/DownloadServlet")
36+
public class DownloadServlet extends HttpServlet {
37+
private static final long serialVersionUID = 1L;
38+
39+
40+
41+
/**
42+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
43+
*/
44+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
45+
downloadData(request, response);
46+
}
47+
48+
/**
49+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
50+
*/
51+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
52+
downloadData(request, response);
53+
}
54+
55+
private void downloadData(HttpServletRequest request, HttpServletResponse response) throws ServletException {
56+
String realPath = request.getServletContext().getRealPath("/images");
57+
String imgName = request.getParameter("name");
58+
String imgPath = realPath + File.separator + imgName;
59+
File f = new File(imgPath);
60+
FileInputStream fis = null;
61+
62+
byte[] buffer = new byte[1024];
63+
64+
response.setContentLength((int) f.length());
65+
66+
try {
67+
fis = new FileInputStream(f);
68+
OutputStream os = response.getOutputStream();
69+
while ( fis.read(buffer) != -1)
70+
os.write(buffer);
71+
}
72+
catch(Throwable t) {
73+
// Handle exception here;
74+
t.printStackTrace();
75+
}
76+
finally {
77+
try {fis.close(); } catch(Throwable t) {}
78+
}
79+
80+
}
81+
82+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2014 Francesco Azzola
3+
* Surviving with Android (http://www.survivingwithandroid.com)
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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+
18+
package com.survivingwithandroid.jee.http;
19+
20+
import java.io.IOException;
21+
import java.util.Enumeration;
22+
23+
import javax.servlet.ServletException;
24+
import javax.servlet.annotation.WebServlet;
25+
import javax.servlet.http.HttpServlet;
26+
import javax.servlet.http.HttpServletRequest;
27+
import javax.servlet.http.HttpServletResponse;
28+
29+
/**
30+
* Servlet implementation class TestServlet
31+
*/
32+
@WebServlet("/TestServlet")
33+
public class TestServlet extends HttpServlet {
34+
private static final long serialVersionUID = 1L;
35+
36+
37+
38+
/**
39+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
40+
*/
41+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42+
System.out.println("### GET ###");
43+
dumpRequest(request, response);
44+
}
45+
46+
/**
47+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
48+
*/
49+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50+
System.out.println("### POST ###");
51+
dumpRequest(request, response);
52+
}
53+
54+
55+
private void dumpRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
56+
Enumeration<String> keys = request.getParameterNames();
57+
System.out.println("Request Parameters:");
58+
while (keys.hasMoreElements()) {
59+
String key = keys.nextElement();
60+
String val = request.getParameter(key);
61+
System.out.println("Key ["+key+"] - Val ["+val+"]");
62+
}
63+
64+
response.getWriter().println("Hello " + request.getParameter("name"));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)