Android 将图像上载到servlet时出错
我正在制作一个android客户端服务器应用程序,其中我正在将一个图像上传到一个托管在JBOSS服务器上的servlet。将pic上传到servlet的Android代码如下所示。。。。bmp是位图格式的图像Android 将图像上载到servlet时出错,android,jboss,objectinputstream,Android,Jboss,Objectinputstream,我正在制作一个android客户端服务器应用程序,其中我正在将一个图像上传到一个托管在JBOSS服务器上的servlet。将pic上传到servlet的Android代码如下所示。。。。bmp是位图格式的图像 if (bmp != null) { HttpClient httpClient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext();
if (bmp != null) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(getString(R.string.postImageUri));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 75, byteArrayOutputStream);
byte[] byteData = byteArrayOutputStream.toByteArray();
ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("image",byteArrayBody);
httpPost.setEntity(builder.build());
try {
HttpResponse httpResponse = httpClient.execute(httpPost,httpContext);
System.out.println(httpResponse.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我正在使用multipart builder将其发送给其他用户。JavaServlet是这样运行的
public class ImageCapture extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("You need to post an image.");
PrintWriter out = response.getWriter();
out.println("Please post an image..");
out.flush();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
long time = System.currentTimeMillis();
System.out.println("Got a post - " + time);
try{
// get the image from the app
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
byte[] imageBytes = (byte[]) ois.readObject();
PlanarImage image = ImageUtil.getAsImage(imageBytes);
ois.close();
System.out.println("Read image from stream...");
// save a copy for inspection
ImageUtil.saveImage(image, time + "_y");
// clean up the image
Restore r = new Restore();
image = r.cleanThroughGlass(image);
System.out.println("Image cleaned...");
System.out.println( getProcessingTime(time, System.currentTimeMillis()) );
// save a copy of cleaned version
ImageUtil.saveImage(image, time + "_x");
// send image back to app
System.out.println("Sending back to client...");
imageBytes = ImageUtil.getAsBytes(image);
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject( imageBytes );
oos.flush();
oos.close();
}catch( Exception e ){
System.out.println("Trouble on the servlet!!!");
e.printStackTrace();
}
System.out.println("Done!");
}
当我试图上传图像时,我遇到以下服务器异常
11:11:53,406 INFO [stdout] (http--0.0.0.0-8080-2) You need to post an image.
11:59:07,535 INFO [stdout] (http--0.0.0.0-8080-2) You need to post an image.
11:59:51,311 INFO [stdout] (http--0.0.0.0-8080-1) Got a post - 1403888391311
11:59:51,314 INFO [stdout] (http--0.0.0.0-8080-1) Trouble on the servlet!!!
11:59:51,315 ERROR [stderr] (http--0.0.0.0-8080-1) java.io.StreamCorruptedException: invalid stream header: 2D2D3375
11:59:51,317 ERROR [stderr] (http--0.0.0.0-8080-1) at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
11:59:51,318 ERROR [stderr] (http--0.0.0.0-8080-1) at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
11:59:51,320 ERROR [stderr] (http--0.0.0.0-8080-1) at VinCapture.doPost(VinCapture.java:39)
11:59:51,321 ERROR [stderr] (http--0.0.0.0-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
11:59:51,322 ERROR [stderr] (http--0.0.0.0-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
11:59:51,323 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
11:59:51,325 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
11:59:51,327 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
11:59:51,328 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
11:59:51,330 ERROR [stderr] (http--0.0.0.0-8080-1) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
11:59:51,332 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
11:59:51,333 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
11:59:51,335 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
11:59:51,337 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
11:59:51,339 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
11:59:51,340 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
11:59:51,342 ERROR [stderr] (http--0.0.0.0-8080-1) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
11:59:51,344 ERROR [stderr] (http--0.0.0.0-8080-1) at java.lang.Thread.run(Thread.java:745)
11:59:51,345 INFO [stdout] (http--0.0.0.0-8080-1) Done!
11:11:53406信息[stdout](http--0.0.0.0-8080-2)您需要发布一个图像。
11:59:07535信息[stdout](http--0.0.0.0-8080-2)您需要发布一个图像。
11:59:51311信息[stdout](http--0.0.0.0-8080-1)获得帖子-1403888391311
11:59:51314信息[stdout](http--0.0.0.0-8080-1)servlet出现故障!!!
11:59:51315错误[stderr](http--0.0.0.0-8080-1)java.io.StreamCorruptedException:无效流头:2d3375
11:59:51317 java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)处的错误[stderr](http--0.0.0.0-8080-1)
11:59:51318 java.io.ObjectInputStream处的错误[stderr](http--0.0.0.0-8080-1)。(ObjectInputStream.java:299)
VinCapture.doPost(VinCapture.java:39)上的11:59:51320错误[stderr](http--0.0.0.0-8080-1)
11:59:51321 javax.servlet.http.HttpServlet.service(HttpServlet.java:754)处的错误[stderr](http--0.0.0-8080-1)
11:59:51322 javax.servlet.http.HttpServlet.service(HttpServlet.java:847)处的错误[stderr](http--0.0.0-8080-1)
11:59:51323 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)上的错误[stderr](http--0.0.0.0-8080-1)
11:59:51325错误[stderr](http--0.0.0.0-8080-1)位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
11:59:51327 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)上的错误[stderr](http--0.0.0.0-8080-1)
11:59:51328 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)上的错误[stderr](http--0.0.0.0-8080-1)
11:59:51330 org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)上的错误[stderr](http--0.0.0.0-8080-1)
11:59:51332错误[stderr](http--0.0.0-8080-1)位于org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
11:59:51333错误[stderr](http--0.0.0.0-8080-1)位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
11:59:51335错误[stderr](http--0.0.0-8080-1)位于org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
11:59:51337错误[stderr](http--0.0.0-8080-1)位于org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
11:59:51339错误[stderr](http--0.0.0-8080-1)位于org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
11:59:51340错误[stderr](http--0.0.0-8080-1)位于org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
11:59:51342错误[stderr](http--0.0.0.0-8080-1)位于org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
11:59:51344 java.lang.Thread.run(Thread.java:745)处的错误[stderr](http--0.0.0.0-8080-1)
11:59:51345信息[标准输出](http--0.0.0.0-8080-1)完成!
如果不知道是哪一行导致了异常,请在所有代码行之间添加println()
语句。再次运行并发布以“Got a post”开头的日志猫。主要是ObjectInputStream ois=new ObjectInputStream(request.getInputStream());是导致错误的原因,然后使用注释掉的以下所有行执行测试。