将图像上载到servlet时出错

时间:2014-07-02 16:13:25

标签: android jboss objectinputstream

我正在创建一个android客户端 - 服务器应用程序,我将图像上传到托管在JBOSS服务器上的servlet。用于将pic上传到servlet的Android代码就是这样.... bmp是BITMAP格式的图像......

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来发送它。 JAVA servlet是这样的......

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!

0 个答案:

没有答案