Mohammad Daei
Mohammad Daei

Reputation: 166

How to upload image from android to java

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?

Upvotes: 0

Views: 83

Answers (3)

Mohammad Daei
Mohammad Daei

Reputation: 166

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());
        }

Upvotes: 0

Kiran Choudhary
Kiran Choudhary

Reputation: 77

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

Upvotes: 1

chiastic-security
chiastic-security

Reputation: 20520

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.

Upvotes: 1

Related Questions