简体   繁体   English

图像上传问题从android到php服务器

[英]Image Uploading issue from android to php server

I am uploading some images from android to server using php. 我正在使用php将一些图像从android上传到服务器。 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 上传是在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. 为图像而不是multipart写字节数组或base64格式将是简单的解决方案。

Check this : Uploading image from android to php server 检查一下: 将图像从android上传到php服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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