1

次の Android / PHP コードを使用して、ファイルをサーバーにアップロードしています。元のファイル名にスペースが含まれていない限り、すべて正常に機能し、その時点で失敗します。

Android: (問題のスペースは文字列パス変数にあります)

public static void uploadFile(String path,String group,String folder) {



        HttpURLConnection conn = null;


            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            try {
                // ------------------ CLIENT REQUEST



                FileInputStream fileInputStream = new FileInputStream(new File(
                        path));





                // open a URL connection to the Servlet

                URL url = new URL("http://uploadsite.php" +
                        "?group_name=" + group + "&folder=" + folder  );

                // 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);



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

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                                + path + "" + lineEnd);
                dos.writeBytes(lineEnd);
                // create a buffer of maximum size



                int bytesAvailable = fileInputStream.available();
                int maxBufferSize = 1*1024*1024;

                byte[] buffer = new byte[bytesAvailable];

                // read file and write it into form...

                int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

                while (bytesRead > 0) {
                    dos.write(buffer, 0, bytesAvailable);
                    bytesAvailable = fileInputStream.available();
                    bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
                }

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                // close streams

                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (MalformedURLException ex) {
                Log.e(TAG, "error: " + ex.getMessage(), ex);
            }

            catch (IOException ioe) {
                Log.e(TAG, "error: " + ioe.getMessage(), ioe);
            }

            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                        .getInputStream()));
                String line;
                while ((line = rd.readLine()) != null) {
                    Log.e(TAG, "Message: " + line);
                }
                rd.close();

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

PHP:

<?php

 $group = $_GET["group_name"];
 $folder = $_GET["folder"];
 $base_path  = "./db/";

 $path = $base_path . $group;



$target_path = $path . "/" . $folder . "/" . basename( $_FILES['uploadedfile']['name']);

$group = ( $_FILES['group_name']['name']);



$file = basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
    chmod ($path.basename( $_FILES['uploadedfile']['name']), 0777);
} else{
    echo "There was an error uploading the file, please try again!(".basename( $_FILES['uploadedfile']['name']).")";
}
?>

Android側でファイル名を変更せずにこのファイルをアップロードする方法について何か提案はありますか?

ありがとうジョシュ

4

2 に答える 2