我正在尝试从Java应用程序上传二进制文件,并通过服务器端的php脚本处理它。
但是当我在Eclipse(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>
";
}
&GT;
这是我的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();
}
}
}
这是错误日志:
byte Transferred = 8802304
byte Transferred = 8804659
警告:未知:POST内容长度8804659字节超过了 0 0 未知中的8388608字节限制
警告:无法修改标题信息 - 已在 0 未知中发送的标题左侧
警告:无法修改标题信息 - 已在 2 C:\ wamp \ www \ testhttpfileupload3.php 中发送的标题
注意:未定义变量: C:\ wamp \ www \ testhttpfileupload3.php 中 34 的文件名
注意:未定义的变量: C:\ wamp \ www \ testhttpfileupload3.php 中的行 34
警告:fopen(./ uploaded_file)[function.fopen]:无法打开流: C:\ wamp \ www \ testhttpfileupload3.php 中没有此类文件或目录 34
<?xml version="1.0" encoding="UTF-8"?>
<data>
<status type="file_upload">
<value>ok</value>
</status>
</data>
答案 0 :(得分:2)
看起来您遇到了PHP启动错误,该错误在假想的第0行(在执行任何代码之前)输出。
警告Unknown: POST Content-Length of 8804659 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
正在header
来电之前输出,因此无法正常工作。
您需要增加允许的最大大小,使用隐藏的MAX_FILE_SIZE
输入字段 1 ,或者在php.ini
(display_startup_errors)中禁用启动错误。在生产中,错误显示应该关闭。但主要的问题是你试图上传一个大于PHP的文件被设置为允许,所以即使没有那个错误,文件上传也不完整。
答案 1 :(得分:1)
写出的警告消息会导致初始http标头被发送。您之后给出的标题命令不再起作用,因为您已经输出了该警告。
解决方案是更改上传后限制,或在display_errors
中停用php.ini