Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cakephp将图像从android上传到服务器_Android_Image_Cakephp_Upload - Fatal编程技术网

使用cakephp将图像从android上传到服务器

使用cakephp将图像从android上传到服务器,android,image,cakephp,upload,Android,Image,Cakephp,Upload,我正在编写一个android应用程序,允许将图像上传到服务器(使用Cakephp)。我可以使用纯php,如以下解决方案: 我的问题是,我不知道如何处理我的控制器将文件移动到cakephp的img目录。我已经尝试使用下面的代码,但它不起作用。非常感谢你的助手 $target_path = "./"; $target_path = $target_path . basename( $_FILES['picture']['name']); if(move_uploaded_file($_FILES[

我正在编写一个android应用程序,允许将图像上传到服务器(使用Cakephp)。我可以使用纯php,如以下解决方案: 我的问题是,我不知道如何处理我的控制器将文件移动到cakephp的img目录。我已经尝试使用下面的代码,但它不起作用。非常感谢你的助手

$target_path  = "./";
$target_path = $target_path . basename( $_FILES['picture']['name']);
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['picture']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}
这是android上的代码

try {

            // open a URL connection to the Server
            FileInputStream fileInputStream = new FileInputStream(newFile);
            URL url = new URL(url_upload);

            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("profilepicture",
                    newFile.getAbsolutePath());

            dos = new DataOutputStream(conn.getOutputStream());

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes(("Content-Disposition: form-data; name=\"picture\";filename=\""
                    + newFile.getAbsolutePath() + "\"" + 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 multiple part form data necessary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            if (serverResponseCode == 200) {

                Log.i("uploadFile", "HTTP Response is : "
                        + serverResponseMessage + ": " + serverResponseCode);
            }
            // after download show image in gallery
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));

            // close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            ex.printStackTrace();
            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Upload file to server Exception",
                    "Exception : " + e.getMessage(), e);
        }

但它不起作用
-它以什么方式不起作用?在php中工作的解决方案将在CakePHP中工作(即使不是最合适的),因为Cake是用。。。php。与原始php相比,使用Cake的唯一真正区别在于您应该参考
$this->request->data
$this->data in older versions
),而不是使用
$\u文件
全局。谢谢,我发现了错误。这是因为我没有在目标目录上写入文件的权限。现在我做到了。