我写了一个代理来拦截我的tomcat的http请求。使用TCP/IP拦截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
{
}
}
当您仅使用`InputStream` /`OutputStream`(因此**没有**`Reader` /`Writer`)时,编码是一个非问题。至于具体问题,如果您更新问题以包含尽可能最小的代码片段(重新产生问题),并附有实际输入和输出字节的实例,将会有所帮助。 – BalusC 2011-02-13 05:10:46
为什么不通过过滤器来实现? – 2011-02-13 05:21:59