我已经编写了一个代理来拦截我的 tomcat 的 http 请求。
每个请求都会通过我的代理并在到达 tomcat 服务器之前进行一些检查。我通过使用用java编写的TCP/IP绑定端口来做到这一点。
除了文件上传(多部分 POST 表单)提交之外,所有请求(GET 和 POST)都能够成功路由到 tomcat 服务器。
即使我能够获取 TCP/IP 中的所有字节并且能够将数据刷新回 tomcat 服务器,但数据会以某种方式被截断/丢失
在处理文件流内容时,我需要做一些特殊的事情,比如编码等吗?
下面是我的示例代码...
protected void processData(InputStream input, OutputStream output) throws IOException
{
// reads a line of text from an InputStream
StringBuffer data = new StringBuffer("");
StringBuffer data2 = new StringBuffer("");
StringBuffer data3 = new StringBuffer("");
StringBuffer data4 = new StringBuffer("");
int c;
try
{
while ((c = input.read()) >= 0)
{
data.append((char) c);
// check for an end-of-line character
if ((c == 0) || (c == 10) || (c == 13))
{
output.write(data.toString().getBytes(), 0, data.length());
data4.append(data.toString());
data = new StringBuffer();
count = 0;
}
else
{
if (count > 6)
{
if (input.available() == 1)
{
data.append((char) input.read());
}
data2.append(data.toString());
data4.append(data.toString());
output.write(data.toString().getBytes(), 0, data
.toString().length());
data = new StringBuffer();
}
else
{
if (count == 6)
{
if (data.toString().toLowerCase()
.indexOf("get /") == 0
|| data.toString().toLowerCase()
.indexOf("post /") == 0)
{
count = 0;
contentLength = -1;
// continue read data(header info)
while ((line = readLine(input, data)) != null)
{
data = new StringBuffer();
// do my own stuff here dealing with headers
if (StringUtils.isBlank(line))
{
data4.append(line);
output.write(line.getBytes(), 0,
line.length());
break;
}
line += "\r\n";
output.write(line.getBytes(), 0,
line.length());
data4.append(line);
output.flush();
}
}
else
{
if (input.available() == 1)
{
data.append((char) input.read());
}
}
}
else
{
if (input.available() == 1)
{
data.append((char) input.read());
output.write(data.toString().getBytes(), 0,
data.toString().length());
data4.append(data.toString());
data3.append(data.toString());
data = new StringBuffer();
}
}
}
count++;
}
if (processbody)
total++;
if (contentLength > 0 && contentLength == total)
{
log.debug("post data2: "
+ (data2.toString() != null ? data2.toString() : " "));
log.debug("post data3: "
+ (data3.toString() != null ? data3.toString() : " "));
log.debug("post data4: "
+ (data4.toString() != null ? data4.toString() : " "));
output.flush();
}
}
}
catch (Exception e)
{
log.error("Error ", e);
}
finally
{
}
}