将字节数组流式传输到Google App Engine servlet

时间:2011-10-31 19:04:24

标签: java google-app-engine

我正在尝试以byte []的形式将图像流式传输到Google App Engine servlet。我之前已经在Tomcat上运行servlet,但由于某些原因,使用GAE这样做似乎更成问题。

字节数组从客户端正常流式传输并且大小正确,但在服务器端读取时总是为空。

以下是从客户端进行流式传输的重要代码片段:

URL myURL = new URL("http://myapp.appspot.com/SetAvatar?memberId=1");

URLConnection servletConnection = myURL.openConnection();

servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);

OutputStream os = servletConnection.getOutputStream();
InputStream is = servletConnection.getInputStream();

IOUtils.write(imageBytes, os);

os.flush();
os.close();

BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;

while ((inputLine = in.readLine()) != null) {
  System.out.println(inputLine);
}

in.close();

以下是GAE servlet中的代码:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  BufferedReader reader = request.getReader();
  byte[] imageBytes = IOUtils.toByteArray(reader);

  PrintWriter outputWriter = response.getWriter();

  int len = request.getContentLength();
  outputWriter.println("Content type is: " + request.getContentType());
  outputWriter.println("Content length is: " + request.getContentLength());
  outputWriter.println("Bytes read: " + imageBytes.length);

  outputWriter.close();
}

服务器的输出是:

Content type is: application/octet-stream
Content length is: 0
Bytes read: 0

我尝试过几乎所有类似读者和流的内容,但总是有相同的结果:服务器端的空字节数组。我正在使用Apache Commons IO软件包中的IOUtils类,但我也试过了。

为什么会发生这种情况?提前感谢任何线索!

0 个答案:

没有答案