简体   繁体   English

关于将图像文件从android上传到服务器的问题

[英]Issue regarding the uploading of image files in to server from android

I have created an application, using which I can upload an image stored at the gallery of my phone to the server. 我创建了一个应用程序,可以使用该应用程序将手机图库中存储的图像上传到服务器。 The server side language that I am using is PHP. 我正在使用的服务器端语言是PHP。 The issue that I am facing is little bit strange. 我面临的问题有点奇怪。 I am able to upload photos of size less that 2 MB, which I have taken using the camera of my phone. 我可以上传小于2 MB的照片,这些照片是使用手机的相机拍摄的。 But certain photos are not getting uploaded. 但是某些照片没有上传。 Here are the details of photos which were not getting uploaded: 以下是未上传的照片的详细信息:

First photo: 第一张照片:

Title: 2013-03-15 09.17.22.jpg 标题:2013-03-15 09.17.22.jpg

Type: JPEG 类型:JPEG

Size: 2645Kb 大小:2645Kb

Second photo: 第二张照片:

Title: 2013-03-12 11.48.04.jpg 标题:2013-03-12 11.48.04.jpg

Type: JPEG 类型:JPEG

Size: 2781Kb 大小:2781Kb

After the unsuccessful uploading of above photos I tried to upload another photo and it worked. 未能成功上传以上照片后,我尝试上传另一张照片,并且可以正常工作。 Here are the details of the photo that I was able to upload: 以下是我可以上传的照片的详细信息:

Title: 2013-03-14 10.20.16.jpg 标题:2013-03-14 10.20.16.jpg

Type: JPEG 类型:JPEG

Size: 1238Kb 大小:1238Kb

Then tried with few other photos less that 2000 kb and all those photos got uploaded. 然后尝试使用其他一些小于2000 kb的照片,然后上传所有这些照片。

Then I download a jpg image of 6 MB from net and tried to upload it. 然后,我从网上下载了6 MB的jpg图片,并尝试上传。 Surprisingly it got uploaded. 令人惊讶的是它被上传了。 Now I am totally confused, what the real issue is. 现在我完全感到困惑,真正的问题是什么。 Finally I have reached a conclusion that, the problem is only for those photos which are taken using the camera of my phone and having a size more than 2000 kb. 最终,我得出一个结论,问题仅在于使用手机的相机拍摄且尺寸大于2000 kb的照片。

Here is the android function that I am using to upload photos in to the server: 这是我用来将照片上传到服务器的android函数:

public String doFileUpload(String selectedPath,String page)
        {
            String response=null;
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1*1024*1024;
            String urlString = Connector.URL+page;
            try
            {
                //------------------ CLIENT REQUEST
                FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
                // open a URL connection to the Servlet
                URL url = new URL(urlString);
                // Open a HTTP connection to the URL
                conn = (HttpURLConnection) url.openConnection();
                // Allow Inputs
                conn.setDoInput(true);
                // Allow Outputs
                conn.setDoOutput(true);
                // Don't use a cached copy.
                conn.setUseCaches(false);
                // Use a post method.
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
                dos = new DataOutputStream( conn.getOutputStream() );
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data;name=uploadedfile;filename=" + selectedPath + "" + lineEnd);


                dos.writeBytes(lineEnd);
                // create a buffer of maximum size
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                while (bytesRead > 0)
                {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }
                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // close streams
                fileInputStream.close();
                dos.flush();
                dos.close();
            }
            catch (MalformedURLException ex)
            {
                Log.e("Debug", "error: " + ex.getMessage(), ex);
            }
            catch (IOException ioe)
            {
                Log.e("Debug", "error: " + ioe.getMessage(), ioe);
            }
            //------------------ read the SERVER RESPONSE
            try 
            {
              inStream = new DataInputStream ( conn.getInputStream() );
              response = inStream.readLine();
              inStream.close();

            }
            catch (IOException ioex){
                Log.e("Debug", "error: " + ioex.getMessage(), ioex);
            }
            return response;
        }

Here is the PHP code for the page that I use to upload images: 这是我用来上传图片的页面的PHP代码:

<?php
$tt=$_GET["id"];
// Where the file is going to be placed
$target_path = "uploads/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{   
    include("../../classes/Registration.php");
    $Obj=new register();
    $Obj->insert_photo_data($tt,basename( $_FILES['uploadedfile']['name']));
}
else
{
    echo '[{"flag":"0"}]';
}
?>

What I am getting back when the image uploading problem occurs is: 出现图片上传问题时,我得到的是:

[{"flag":"0"}]

Change. 更改。 dos.write(buffer, 0, bufferSize); dos.write(buffer,0,bufferSize); to dos.write(buffer, 0, bytesRead). 到dos.write(buffer,0,bytesRead)。 What reaches the server if a picture is not uploaded? 如果未上传图片,什么到达服务器?

I found the solution. 我找到了解决方案。 The issue was related to wamp. 问题与沼泽有关。 I have opened php.ini and changed the line 我打开了php.ini并更改了该行

upload_max_filesize = 2M

to

upload_max_filesize = 10M

Now images are getting uploaded without any problem. 现在,图像可以顺利上传。

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

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