1

I need to send some byte array from android device to Servlet. For this I try to use next code:

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {

  DataInputStream in = new DataInputStream((InputStream)request.getInputStream());
   response.setContentType("text/plain");
    byte[] buffer = new byte[1024];
    int len = 0;
    File file;
    file=new File(getServletContext().getRealPath("/POST_LOG!!!!.txt"));
    if(!file.exists()){
        file.createNewFile();
    }
    while ((len = in.read(buffer)) > 0) {
          FileOutputStream fos = new FileOutputStream(getServletContext().getRealPath("/POST_LOG!!!!.txt"), true);

           fos.write(buffer);            
           fos.close(); 
    }

    PrintWriter out = response.getWriter();
    out.write("Done");
    out.close();

Device side :

URL uploadUrl;
    try {
        uploadUrl = new URL(url);
        HttpURLConnection c = (HttpURLConnection) uploadUrl
                .openConnection();
        c.setRequestMethod("POST");

        c.setDoInput(true);
        c.setDoOutput(true);
        c.setUseCaches(false);
        c.connect();
        OutputStream out = c.getOutputStream();

        for (int i = 0; i < 1000; i++) { // generate random bytes for
                                            // uploading
            byte[] buffer = new byte[256];
            for (int j = 0; j < 256; j++) {
                Random r = new Random();
                buffer[j] = (byte) r.nextInt();
            }

            out.write(buffer);
            out.flush();
        }

        out.close();

    } catch (Exception e) {
        MessageBox("Error. " + e.toString());
    }

    return (long) 0;
}

I dont understand why this code doesnt work. When I try to debug my POST method it even not called. I will grateful for your examples

1
  • Please share your web.xml entry of servlet configuration, URL mapping and servletn URL you are using to invoke it. Commented Mar 26, 2012 at 6:45

2 Answers 2

2

I found the solution. I just changed my device-side code using custom InputStream.

Device side :

HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new InputStreamEntity(new MyInputStream(),
            4096 * 1024 * 10));
    HttpResponse response = null;

    try {
        response = httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        httpPost.abort();
    } catch (IOException e) {
        e.printStackTrace();
        httpPost.abort();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You have a lot of options:

  • send the byte values: 125,11,25,40 (that's a dumb option)
  • send it base64- or hex- encoded, and then decode it (use apache commons-codec)
  • submit it as multipart/form-data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.