可能重复:
PHP 已发送的标头
我正在尝试从 Java 应用程序上传二进制文件并通过服务器端的 php 脚本处理它。
但是当我在 Eclipse (for Java) 中运行代码时,它会显示以下日志。
虽然我在第一行发送标题信息,但为什么会显示此消息。
这是我的php代码:
<?php
header('Content-type: application/xml');
/*******************************************/
$DIRECTORY_SEPARATOR = "/";
$BUF_SIZE = 4096;
/*******************************************/
uploadFile();
sendResponse();
/*******************************************/
function uploadFile()
{
global $DIRECTORY_SEPARATOR;
global $BUF_SIZE;
$targetDir = "." . $DIRECTORY_SEPARATOR . "uploaded_file";
/***************************************/
// 5 minutes execution time
@set_time_limit(5 * 60);
/***************************************/
// Set filename
$filename="temp_aa.mp3";
/***************************************/
if (!file_exists($targetDir))
{
mkdir($targetDir,0777,true);
}
/***************************************/
$out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
if ($out)
{
$in = fopen("php://input", "rb");
if ($in)
{
while ($buff = fread($in, $BUF_SIZE))
{
fwrite($out, $buff);
}
fclose($in);
}
else
{
fclose($out);
return ;
}
}
else
{
return ;
}
}
function sendResponse()
{
echo "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<data>
<status type=\"file_upload\">
<value>ok</value>
</status>
</data>
";
}
?>
这是我的 Java 端代码:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestHttp
{
public static void main(String[] args)
{
HttpURLConnection httpUrlConnection = null;
File mFileToUpload = new File("c:/aa.mp3");
int byteTrasferred = 0, mBufferSize = 4096, mTempBytesRead = 0;
byte[] mBuffer = new byte[mBufferSize];
try
{
httpUrlConnection = (HttpURLConnection)new URL("http://127.0.0.1/testhttpfileupload3.php").openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
OutputStream os = httpUrlConnection.getOutputStream();
Thread.sleep(1000);
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(mFileToUpload));
while((mTempBytesRead = fis.read(mBuffer)) != -1)
{
os.write(mBuffer, 0, mTempBytesRead);
byteTrasferred += mTempBytesRead;
System.out.println("byteTrasferred = " + byteTrasferred);
}
fis.close();
os.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
String s = null;
while ((s = in.readLine()) != null)
{
System.out.println(s);
}
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
这是错误日志:
字节传输 = 8802304
byte Transferred = 8804659
警告:未知:POST Content-Length of 8804659 bytes 超出第0行未知中 8388608 字节的限制警告:无法修改标头信息 - 标头已在第0行未知中发送警告:无法修改标头信息 - 标头已在第2行的 C: \ wamp\www\testhttpfileupload3.php 中发送注意:未定义的变量:第34行的C:\wamp\www\testhttpfileupload3.php中的文件名注意:未定义的变量:C:\wamp\www\testhttpfileupload3中的块.php在第34行
警告:fopen(./uploaded_file) [function.fopen]:无法打开流:第34行的C:\wamp\www\testhttpfileupload3.php中没有这样的文件或目录
<?xml version="1.0" encoding="UTF-8"?>
<data>
<status type="file_upload">
<value>ok</value>
</status>
</data>