basically what I am trying create a servlet that will receive the bytes that are being sent from the MIDlet. I have pasted my function that is sending the bytes from the MIDlet so we can be on the same page.

If anyone can help please email me at <snipped>
Thank you in advance

// taking in the byte array holding the audio
void sendPostRequest(byte[] requestaudio)
{
HttpConnection hc = null;
DataInputStream dis = null;
DataOutputStream dos = null;


try
{
// Open up a http connection with the Web server
// for both send and receive operations
hc = (HttpConnection)
Connector.open(defaultURL, Connector.READ_WRITE);


// Set the request method to POST
hc.setRequestMethod(HttpConnection.POST);


// Send the audio user byte by byte
dos = hc.openDataOutputStream();
byte[] request_body = requestaudio;


for (int i = 0; i < request_body.length; i++)
dos.writeByte(request_body);


System.out.println("Packaging the bytes: " + request_body.length););



dos.flush();
dos.close();
//dis.close();
}
catch (IOException ioe) {}


finally
{
// Free up i/o streams and http connection
try
{
if (hc != null) hc.close();
}
catch (IOException ignored) {}


try
{
if (dis != null) dis.close();
}
catch (IOException ignored) {}


try
{
if (dos != null) dos.close();
}
catch (IOException ignored) {}
}
}

Dani AI

Generated

Brief diagnosis and practical fixes based on the MIDlet snippet and suggestion.

The client is most likely sending the payload incorrectly (writing the same thing repeatedly or using the wrong write method) and not providing enough HTTP metadata for the server to handle a raw audio stream. Instead of looping and calling a byte-write repeatedly, send the entire buffer once (or stream it once) and set a sensible Content-Type and Content-Length. As suggested, first verify behavior with a very small known file and a simple server-side endpoint that saves the incoming bytes — that isolates network/format problems from servlet logic.

Servlet-side: accept the POST body as a raw stream and stream it straight to disk (do not slurp into a single large in-memory byte[] unless the file is tiny). Example doPost pattern:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    InputStream in = req.getInputStream();
    File out = new File("/var/tmp/uploaded_audio.wav"); // choose safe path
    try (FileOutputStream fos = new FileOutputStream(out)) {
        byte[] buf = new byte[4096];
        int r;
        while ((r = in.read(buf)) != -1) fos.write(buf, 0, r);
    }
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.getWriter().println("saved");
}

Troubleshooting checklist

  • Confirm the MIDlet sets a Content-Type (e.g., audio/wav or application/octet-stream) and, when possible, Content-Length.
  • Replace per-byte loop logic with a single write(byte[]) or a proper byte-range write to avoid mangled or enormous output.
  • Test with curl or a tiny PHP script to ensure the server endpoint correctly receives the file before debugging servlet/container issues.
  • Check servlet container permissions and the final file with an audio player to confirm validity.
  • Log request headers and content length on the server to see what the client actually sent.

These steps should isolate whether the problem is client-side encoding/writes or server-side reading/permissions.

Hi there, Have you tested that this code perfectly sends data to the server? i doubt...

I would have tested it but I don't have the data...

Try checking it by accepting it with some simple server side script like php etc. if it works then move to servlet..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.