简体   繁体   中英

Image Uploading issue from android to php server

I am uploading some images from android to server using php. The image file is getting uploaded but when I try to view that file its showing broken image don't know what exactly is the issue. The upload is done in the AsyncTask Android Code

    FileInputStream fileInputStream;
    HttpURLConnection connection;
    URL url;
    DataOutputStream outputStream;
    int bytesRead,bytesAvailable,bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File selectedFile = new File(selectedImagePath);
try
        {
            fileInputStream = new FileInputStream(selectedFile);
            url = new URL("The url of my server");
            connection= (HttpURLConnection)url.openConnection();
            Log.v(TAG,"connection established");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection","Keep-Alive");
            connection.setRequestProperty("ENCTYPE","multipart/form-data");
            connection.setRequestProperty("Content-Type","multipart/form-data;boundary=*****");
            connection.setRequestProperty("uploaded_file",selectedImagePath);

            outputStream = new DataOutputStream(connection.getOutputStream());

            outputStream.writeBytes("--*****\r\n");

            outputStream.writeBytes("Content-Disposition:form-data;name=\"uploaded_file\";filename=\""+selectedImagePath+"\""+"\r\n");
            outputStream.writeBytes("\r\n");
            outputStream.writeBytes("--*****\r\n");
            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);
            }
            outputStream.writeBytes("\r\n");
            outputStream.writeBytes("--*****\r\n");
            int  serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            if(serverResponseCode  == HttpURLConnection.HTTP_OK) {
                String line;
                StringBuilder sb = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }


            }
            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
PHP Code:
 $file_path = basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path) )
{
    echo "success";
} else
{
    echo "fail";
}

Image shown on server: 在此输入图像描述

To write byte array or base64 format for image instead of multipart would be easy solution.

Check this : Uploading image from android to php server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM