Apache サーバーをセットアップし、さまざまな投稿要求を処理するために php スクリプトを実行しています。スクリプトは次のようになります。
$uploaddir = realpath('mypath');
//realpath('./') . '/';
$uploadfile = $uploaddir . '/' . basename($_FILES['file_contents']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
echo "\n<hr />\n";
print_r($_POST);
print "</pr" . "e>\n";
これはうまく機能し、リクエストを処理し、ファイルを必要な場所に移動します。
しかし、次のJavaクライアントでこれを試してみると、何も得られません:
String URL = "http://localhost/accept.php";
File file = new File("C:/Ozer/ANPRProject/MDT/Export/test.txt");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
try{
System.out.println(""+file.exists());
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file),-1);
reqEntity.setContentType("application/octet-stream");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
System.out.println("execute request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
System.out.println("Chunked: " + resEntity.isChunked());
}
EntityUtils.consume(resEntity);
} catch(Exception e) {
} finally {
httpclient.getConnectionManager().shutdown();
}
}
システム出力として得られるものは次のとおりです。
true
execute request POST http://localhost/accept.php HTTP/1.1
----------------------------------------
HTTP/1.1 200 OK
Response content length: 330
Chunked: false
チャンクがtrueに設定されていないので、私は奇妙だと思いますか? 私は自分のコードで具体的にそれを求めましたが。しかし、とにかく、アップロードは機能しません。contentType を「application/octet-stream」に設定しました。これは、投稿をリクエストするために使用した PHP スクリプトがフィードバックとして提供したためです: [type] => application/octet-stream
したがって、Apache のログは、接続が成功し、実際にリクエストを受け取ったことを示しています。
127.0.0.1 - - [08/Aug/2013:16:10:04 +0200] "POST /accept.php HTTP/1.1" 200 330 "-" ""Apache-HttpClient/4.2.5 (java 1.5)"
これは、PHP スクリプトを使用して投稿をリクエストしたときに得られるものです。
127.0.0.1 - - [08/Aug/2013:15:54:23 +0200] "POST /accept.php HTTP/1.1" 200 419 "-" "-" ::1 - - [08/Aug/2013:15:54:23 +0200] "GET /testSendDat.php HTTP/1.1" 200 3330 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
そしてphpスクリプトからのエラーログは私を示しています:
[08-Aug-2013 16:10:04 Europe/Berlin] PHP Notice: Undefined index: file_contents
つまり、ファイルを読み取ろうとしている配列が空です。しかし、なぜ?私は見当もつかない。リクエストを投稿するのに間違った方法を使用していると思いますが、しばらく探していて、問題が何であるかを見つけることに実際には成功していません. 助けは非常に義務付けられます。
ああ、Apache Tomcat サーバーをセットアップして httpservlets を操作する方が簡単でしょうか? それともそれは問題ではないでしょうか?