0

我创建了一个应用程序,使用它可以将存储在手机图库中的图像上传到服务器。我使用的服务器端语言是 PHP。我面临的问题有点奇怪。我可以上传使用手机相机拍摄的小于 2 MB 的照片。但是某些照片没有上传。以下是未上传照片的详细信息:

第一张照片:

标题:2013-03-15 09.17.22.jpg

类型:JPEG

大小:2645Kb

第二张照片:

标题:2013-03-12 11.48.04.jpg

类型:JPEG

大小:2781Kb

在上传上述照片不成功后,我尝试上传另一张照片,它成功了。以下是我能够上传的照片的详细信息:

标题:2013-03-14 10.20.16.jpg

类型:JPEG

大小:1238Kb

然后尝试使用少于 2000 kb 的其他几张照片,所有这些照片都上传了。

然后我从网上下载了一张 6 MB 的 jpg 图片并尝试上传。莫名其妙就上传了。现在我完全糊涂了,真正的问题是什么。最后我得出一个结论,问题只在于那些使用我手机的相机拍摄的并且大小超过 2000 kb 的照片。

这是我用来将照片上传到服务器的 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;
        }

这是我用来上传图片的页面的 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"}]';
}
?>

当图像上传问题发生时,我得到的是:

[{"flag":"0"}]
4

2 回答 2

1

改变。dos.write(buffer, 0, bufferSize); 到 dos.write(buffer, 0, bytesRead)。如果图片没有上传,什么会到达服务器?

于 2013-03-18T21:00:05.463 回答
0

我找到了解决方案。这个问题与 wamp 有关。我打开了 php.ini 并更改了行

upload_max_filesize = 2M

upload_max_filesize = 10M

现在图片上传没有任何问题。

于 2013-03-19T07:22:01.633 回答