Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:06:17+00:00 2026-06-01T00:06:17+00:00

I need to send some byte array from android device to Servlet. For this

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:06:19+00:00Added an answer on June 1, 2026 at 12:06 am

    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();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send some arguments from the iPhone to a php in the
I need to send some files from different machines to only one Server. As
I need to send an image from the Windows Phone 7 to some e-mail
I need to Send Image from silverlight to WCF service. I try to do
I need to send some pretty long strings from a flash application to a
I need to send some data from a SQL DB Server on an internal
I need to send some sets of code which user enters through e-mail. The
I need to send some information on a VxWorks message queue. The information to
I'm building a fairly simple PHP script that will need to send some emails
I need to implement some form of communication mechanism in my application, to send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.