有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java流字节数组到Google应用程序引擎servlet

我正在尝试以字节[]的形式将图像流式传输到Google应用程序引擎servlet。我以前在Tomcat上运行servlets时就这样做过,但出于某种原因,在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

我尝试了几乎所有的方法,比如不同的读卡器和流,但总是得到相同的结果:服务器端有一个空字节数组。我正在使用ApacheCommons IO包中的IOUtils类,但我也尝试过不使用它

知道为什么会这样吗?提前感谢您提供的任何线索


共 (0) 个答案