前提・実現したいこと
android端末からCSVファイルをPHPに POSTしてファイルを保存したいのですが、うまくPHP側で受け取ることができていません。Wiresharkを使い通信を覗いたところandroi端末からPHP側のサーバには通信できていることがわかったので、受け取るkeyを設定していないからなのか?と考えております。
いろいろ調べてみるとリクエストボディの書き込みをしないといけないぽい事が分かったのですがどのように書けばいいかわかりません。JSON文字列とかもよくわかりません。
PHP側でやる場合はフォームの作成で
<input type="text" name="text1">
と書き
フォームの取得で
と書けばフォームから値がtaxt1に書き込まれ送信した際データが取得できるんですが android側でフォームの処理を行うにはどうすればよいのでしょうか。
keyは'uploaded_file'にしようと考えております。
発生している問題・エラーメッセージ
PHP
1Notice: Undefined index: uploaded_file in /var/www/html/UploadToServer.php on line 3 2 3Notice: Undefined index: uploaded_file in /var/www/html/UploadToServer.php on line 4 4エラーが発生しました。
該当のソースコード(1)
android(java)ファイル //androidにあるファイルをサーバにアップロードします public int uploadFile(final String selectedFilePath) { int serverResponseCode = 0; HttpURLConnection connection ; // DataOutputStream dataOutputStream ;//アプリケーションはプリミティブ型のJavaデータを移植性のある形で出力ストリームに書き込むことができます。アプリケーションはデータ入力ストリームを使ってデータを読み戻すことができます。 String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; File selectedFile = new File(selectedFilePath); String[] parts = selectedFilePath.split("/"); final String fileName = parts[parts.length - 1]; if (!selectedFile.isFile()) { dialog.dismiss(); runOnUiThread(new Runnable() { @Override public void run() { tvFileName.setText("Source File Doesn't Exist: " + selectedFilePath); } }); return 0; } else { try { FileInputStream fileInputStream = new FileInputStream(selectedFile); //CSVデータをストリームに書き込むバイト数を取得 DataOutputStream dataOutputStream ; //アプリケーションはプリミティブ型の Java データを移植性のある形で出力ストリームに書き込むことができます URL url = new URL(SERVER_URL); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true);//入力を許可 connection.setDoOutput(true);//出力を許可する connection.setUseCaches(false);//キャッシュされたコピーを使用しないでください connection.setRequestMethod("POST"); connection.setRequestProperty("ENCTYPE", "multipart/form-data");//データをマルチパート形式で送信します(複数の種類のデータを持つ) connection.setRequestProperty("Connection", "Keep-Alive"); //コスト削減方法 connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);//ファイルの種類を表す情報が入ってるよ connection.setRequestProperty("uploaded_file", selectedFilePath); //新しいデータ出力ストリームの作成 dataOutputStream = new DataOutputStream(connection.getOutputStream()); //バイトをデータ出力ストリームに書き込む dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd); dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + selectedFilePath + "\"" + lineEnd); dataOutputStream.writeBytes(lineEnd); dataOutputStream.flush(); //コンピューターでデータを一時的に記憶する場所 bytesAvailable = fileInputStream.available(); //バッファサイズを使用可能な最小バイトまたは1 MBとして選択 bufferSize = Math.min(bytesAvailable, maxBufferSize); // bufferSizeのサイズのバイト配列としてバッファを設定します buffer = new byte[bufferSize]; // FileInputStreamからバイトを読み取ります(バッファーの0番目のインデックスからbuffersizeまで) bytesRead = fileInputStream.read(buffer, 0, bufferSize); // bytesRead = -1になるまでループを繰り返します。つまり、読み取るバイトがなくなる while (bytesRead > 0) { //入力ストリームから読み取ったバイトを書き込みます dataOutputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } dataOutputStream.writeBytes(lineEnd); dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); //get.ResponsCode() HTTP 応答メッセージから状態コードを取得します serverResponseCode = connection.getResponseCode(); String serverResponseMessage = connection.getResponseMessage(); Log.i(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode); //応答コード200は、サーバーステータスがOK if (serverResponseCode == 200) { runOnUiThread(new Runnable() { @Override public void run() { tvFileName.setText("ファイルの Upload が成功しました.\n\n 送信先 \n\n" + "http://192.168.2.117\n\n" + fileName); } }); } //入力ストリームと出力ストリームを閉じます fileInputStream.close(); dataOutputStream.flush(); dataOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "File Not Found", Toast.LENGTH_SHORT).show(); } }); } catch (MalformedURLException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "URL error!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "Cannot Read/Write File!", Toast.LENGTH_SHORT).show(); } dialog.dismiss(); return serverResponseCode; } } }
該当のソースコード(2)
<?php $target_dir = "./upfile/"; $target_path = $target_dir . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploaded_file']['name']). " アップロードができました"; } else{ echo "エラーが発生しました。"; } ?> ~
試したこと
wiresharkで通信を覗いたところ android端末と自サーバとの通信はできていた。
回答1件
あなたの回答
tips
プレビュー