Question

I am facing problem while trying to upload a zip file containing images converted into byte array to a restful wcf service from a json client using HTTPPost. The byte array is encoded into BASE64 enclosed into JSON object and sent using StringEntity with 2 more parameters. Around 6KB of file gets uploaded without any flaws but file more than 6KB are not send and I get a Bad Request - 400 status code. Following code is used to upload the file:

File file = new File(dir, "file.zip");

            byte[] buf = new byte[10240000];

            fis = new FileInputStream(file);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); 
                Log.v("read : buf ", buf + " : " + readNum + " bytes");
            }

            byte[] bytes = bos.toByteArray();
            imgData = Base64.encodeToString(bytes, Base64.DEFAULT);
            JSONObject sendData=null;
            Log.d("Image Data length", imgData.length()+"");
            Log.d("Image data ", imgData);

            try {

                sendData= new JSONObject();
                sendData.put("_binaryData", imgData);
                sendData.put("_fileName", "fileName");
                sendData.put("userid", userID);

                int len = imgData.length();
                int l=sendData.toString().length();
                entity = new StringEntity(sendData.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
            // Send request
            int len = imgData.length();
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
            request.setHeader("Connection", "Keep-Alive");
            request.setParams(httpParameters);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            request.setEntity(entity);
            HttpResponse response = httpClient.execute(request);

            HttpEntity responseEntity = response.getEntity();
            String str=response.getStatusLine().getReasonPhrase();

            int i=response.getStatusLine().getStatusCode();
            Log.v("ReasonPhrase :: StatusCode",str+" "+i);
            int contentLength = (int) responseEntity.getContentLength();
            char[] buffer = new char[(int) responseEntity
                    .getContentLength()];
            InputStream stream = responseEntity.getContent();

Please help me in solving this.

No correct solution

OTHER TIPS

If a message with <6k bytes does through, but messages with >6k don't, I'd take a look at the client and host limits for things like:

    MaxBufferSize 
    MaxBufferPoolSize
    MaxReceivedMessageSize 

You don't say whether or not you have control over the host server settings, but you can increase and decrease the limits on items like those mentioned earlier. You can set them to Integer.Max if necessary, a size that would allow file uploads > 1 GB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top