当我们使用arcgis api for javascript 查询arcgis服务时,如果查询的参数很长时,通过get方式提交会查询不到结果,因为get方式提交的参数有长度限制,需要通过代理的方式使用post提交。
arcgis 提供了一种解决方案就是proxy.jsp.
首先在项目页面js中引入 proxy.jsp:esriConfig.defaults.io.proxyUrl=http://servername:prot/arcgis_js_api/proxy.jsp 引入代理页面
esriConfig.defaults.io.alwaysUseProxy=true; 设置代理是否一直起作用
引入之后把proxy.jsp放入到项目中。
代理页面源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<%@page session=
"false"
%>
<%@page
import
=
"java.net.*,java.io.*"
%>
<%!
String
[] serverUrls = {
"http://servername:8399/arcgis/rest/services"
//对什么服务起作用,可以配置多个用
","
分割。
};
try
{
String
reqUrl = request.getQueryString();
boolean allowed =
false
;
String
token =
null
;
for
(
String
surl : serverUrls) {
String
[] stokens = surl.split(
"\\s*,\\s*"
);
if
(reqUrl.toLowerCase().contains(stokens[
0
].toLowerCase())) {
allowed =
true
;
if
(stokens.length >=
2
&& stokens[
1
].length() >
0
)
token = stokens[
1
];
break
;
}
}
if
(reqUrl.endsWith(
".png"
)) allowed =
true
;
if
(!allowed) {
response.setStatus(
403
);
return
;
}
if
(token !=
null
) {
reqUrl = reqUrl + (reqUrl.indexOf(
"?"
) > -
1
?
"&"
:
"?"
) +
"token="
+ token;
}
URL url =
new
URL(reqUrl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(
true
);
con.setRequestMethod(request.getMethod());
if
(request.getContentType() !=
null
) {
con.setRequestProperty(
"Content-Type"
, request.getContentType());
}
con.setRequestProperty(
"Referer"
, request.getHeader(
"Referer"
));
int
clength = request.getContentLength();
if
(clength >
0
) {
con.setDoInput(
true
);
InputStream istream = request.getInputStream();
OutputStream os = con.getOutputStream();
final
int
length =
5000
;
byte[] bytes =
new
byte[length];
int
bytesRead =
0
;
while
((bytesRead = istream.read(bytes,
0
, length)) >
0
) {
os.write(bytes,
0
, bytesRead);
}
}
else
{
con.setRequestMethod(
"GET"
);
}
out.clear();
out = pageContext.pushBody();
OutputStream ostream = response.getOutputStream();
response.setContentType(con.getContentType());
InputStream
in
= con.getInputStream();
final
int
length =
5000
;
byte[] bytes =
new
byte[length];
int
bytesRead =
0
;
while
((bytesRead =
in
.read(bytes,
0
, length)) >
0
) {
ostream.write(bytes,
0
, bytesRead);
}
}
catch
(Exception e) {
response.setStatus(
500
);
}
%>
|
本文转自 stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1342865