0

I am developing a software in which images should be upload from android to java. so far I have developed the following client on android:

        String url=params[0];
        String filePath=params[1];

        File file=new File(filePath);

        MultipartEntityBuilder multipartEntityBuilder=MultipartEntityBuilder.create();
        multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntityBuilder.addPart("file", new FileBody(file));
        HttpPut httpPut=new HttpPut(url);
        HttpClient httpclient = new DefaultHttpClient();
        httpPut.setEntity(multipartEntityBuilder.build());
        HttpResponse response;

        try {
            response = httpclient.execute(httpPut);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                instream.close();
                return result;
            }
        } catch (Exception e) {
        }
        return null;
    }

and the server is:

String path=Server.imagesPath+Utilities.getRandomString(10)+".jpg";
    InputStream inputStream= arg0.getRequestBody();
    File file=new File(path);
    Files.copy(inputStream, file.toPath());
    String response="OK";
    try{
    arg0.sendResponseHeaders(200, response.length());
        OutputStream outputStream=arg0.getResponseBody();
        outputStream.write(response.getBytes());
        outputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

The image is completely copied to the server but it is damaged and it can not be viewed. What is the problem?

6
  • 1
    What does "from Android to Java" mean? Commented Nov 17, 2014 at 9:43
  • it is damaged. In which way? Did you compare filelength and original filelength? Please tell. What kind of server is this? You are sure you have to PUT? Not POST? Commented Nov 17, 2014 at 10:14
  • arg0 ? What is that? Commented Nov 17, 2014 at 10:19
  • the client which sends the Image is on android and the server which receives the file is in Java. Commented Nov 17, 2014 at 12:12
  • No the file in the destination is bigger than the original file! I am developing both the server an client and I'm developing the server in Java. I'm using PUT on both sides. Actually arg0 is the HttpExchange object passed to the HttpHandler object Commented Nov 17, 2014 at 12:27

3 Answers 3

1

It looks as though the upload is being done as a multipart form, which allows you to insert images as part of the data being sent; but the server is reading it as a pure octet stream rather than a multipart form.

You need to choose one or the other. Either interpret it as form data on the server, or just send the data as a stream rather than as form data.

I'd suggest having a look at Apache Commons FileUpload, which will simplify lots of this.

Sign up to request clarification or add additional context in comments.

2 Comments

OK. How can I do that? How can I send the data as a stream on android or treat received data on Java as a form?
@user2355734 I think the link I provided is the best place to start.
1

You can also use multiple data part entity for image uploading

you can see complete demo here: http://niravranpara.blogspot.in/2012/11/upload-video-in-server.html

Comments

0

I could finally solve the problem using this article:

Android Code to Upload & Download large files to server

the code I used on the Android was:

        String urlString=params[0];
        String path=params[1];
        File file=new File(path);
        int maxBufferSize=1024;
        URL url=null;
        try {
            url=new URL(urlString);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpURLConnection connection=null;
        try {
            connection = (HttpURLConnection) url.openConnection();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        try {
            connection.setRequestMethod("PUT");
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        connection.setRequestProperty("Content-Type", "image/jpg");
        OutputStream outputStream=null;
        FileInputStream fileInputStream=null;
        byte[] buffer;
        int bytesRead,bytesAvailable,bufferSize;

        try {
            outputStream=connection.getOutputStream();
            fileInputStream=new FileInputStream(file);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while(bytesRead > 0){
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();
            Log.d("test Response Code ",Integer.toString(serverResponseCode));
            Log.d("test Response Message ", serverResponseMessage);

        } catch (IOException e) {
            Log.d("test", e.getMessage());
        }

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.