$\u文件数组通过Java上载为空

$\u文件数组通过Java上载为空,java,android,httpurlconnection,Java,Android,Httpurlconnection,我正试图通过Android将一个文件上传到PHP,但$\u FILES数组显示为空:array()。我正在使用HttpURLConnection。我已将服务器和客户端的ip地址以及其他个人信息替换为“” 我已验证文件是否确实正在上载(wireshark转储): 这是php.ini文件中的相关部分。我用chmod 777创建了上载目录: ;;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;; ; Whether to allow HTTP file

我正试图通过Android将一个文件上传到PHP,但$\u FILES数组显示为空:
array()
。我正在使用HttpURLConnection。我已将服务器和客户端的ip地址以及其他个人信息替换为“”

我已验证文件是否确实正在上载(wireshark转储):

这是php.ini文件中的相关部分。我用
chmod 777
创建了上载目录:

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = <my_directory>

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 5M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
;;;;;;;;;;;;;;;;
; 文件上传;
;;;;;;;;;;;;;;;;
; 是否允许HTTP文件上载。
; http://php.net/file-uploads
文件上传=打开
; HTTP上载文件的临时目录(如果不是,将使用系统默认值
;具体说明)。
; http://php.net/upload-tmp-dir
上传\u tmp\u目录=
; 上载文件允许的最大大小。
; http://php.net/upload-max-filesize
上载\u最大\u文件大小=5M
; 通过单个请求可以上载的最大文件数
最大文件上传量=20

我猜这与我的java代码如何生成HTTP头有关?我完全迷路了。

好吧,我把它修好了。无论何时涉及HTTP头,都需要小心地发送准确的字符串。例如,我使用了
Content-disposition
而不是
Content-disposition
,并且在每次使用
boundary
字符串之前都没有加上双破折号。我还犯了一个明显的错误,写了
multipart/form
而不是
multipart/form数据
。最后,我包含了一个
内容类型
标题,并在实际数据之前添加了两个返回。以下是用于比较的代码:

String CRLF = "\r\n";
String dashes = "--";
String boundary = "----------j2afevn8a23nfsaj1";

FileInputStream in = new FileInputStream(file);

url = new URL("http://<ip_addr>/test.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");

httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
httpURLConnection.setRequestProperty("Cookie", header);

OutputStream output = httpURLConnection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);

writer.append(dashes + boundary + CRLF);
writer.append("Content-Disposition: form-data; name=\"report\"; filename=\"report.pdf\"" + CRLF);
writer.append("Content-Type: application/pdf" + CRLF);
writer.append(CRLF);

writer.flush();

byte[] buffer = new byte[1024];
int len;

while((len = in.read(buffer)) != -1){
    output.write(buffer);
}

output.flush();
writer.append(CRLF);
writer.append(dashes + boundary + CRLF);
writer.append(CRLF);
writer.flush();
writer.close();
output.close();

code = httpURLConnection.getResponseCode();

if(code == 401){
    message = "Error.";
}   

httpURLConnection.disconnect();
String CRLF=“\r\n”;
字符串破折号=“--”;
字符串边界=“------------j2afevn8a23nfsaj1”;
FileInputStream in=新的FileInputStream(文件);
url=新url(“http:///test.php");
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
httpURLConnection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
httpURLConnection.setRequestProperty(“Cookie”,标题);
OutputStream output=httpURLConnection.getOutputStream();
PrintWriter writer=新的PrintWriter(新的OutputStreamWriter(输出,“UTF-8”),true;
writer.append(破折号+边界+CRLF);
writer.append(“内容配置:表单数据;名称=\”报告\“文件名=\”报告.pdf \“+CRLF”);
writer.append(“内容类型:application/pdf”+CRLF);
writer.append(CRLF);
writer.flush();
字节[]缓冲区=新字节[1024];
内伦;
而((len=in.read(buffer))!=-1){
输出。写入(缓冲区);
}
output.flush();
writer.append(CRLF);
writer.append(破折号+边界+CRLF);
writer.append(CRLF);
writer.flush();
writer.close();
output.close();
code=httpURLConnection.getResponseCode();
如果(代码==401){
message=“错误。”;
}   
httpURLConnection.disconnect();

好的,我把它修好了。无论何时涉及HTTP头,都需要小心地发送准确的字符串。例如,我使用了
Content-disposition
而不是
Content-disposition
,并且在每次使用
boundary
字符串之前都没有加上双破折号。我还犯了一个明显的错误,写了
multipart/form
而不是
multipart/form数据
。最后,我包含了一个
内容类型
标题,并在实际数据之前添加了两个返回。以下是用于比较的代码:

String CRLF = "\r\n";
String dashes = "--";
String boundary = "----------j2afevn8a23nfsaj1";

FileInputStream in = new FileInputStream(file);

url = new URL("http://<ip_addr>/test.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");

httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
httpURLConnection.setRequestProperty("Cookie", header);

OutputStream output = httpURLConnection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);

writer.append(dashes + boundary + CRLF);
writer.append("Content-Disposition: form-data; name=\"report\"; filename=\"report.pdf\"" + CRLF);
writer.append("Content-Type: application/pdf" + CRLF);
writer.append(CRLF);

writer.flush();

byte[] buffer = new byte[1024];
int len;

while((len = in.read(buffer)) != -1){
    output.write(buffer);
}

output.flush();
writer.append(CRLF);
writer.append(dashes + boundary + CRLF);
writer.append(CRLF);
writer.flush();
writer.close();
output.close();

code = httpURLConnection.getResponseCode();

if(code == 401){
    message = "Error.";
}   

httpURLConnection.disconnect();
String CRLF=“\r\n”;
字符串破折号=“--”;
字符串边界=“------------j2afevn8a23nfsaj1”;
FileInputStream in=新的FileInputStream(文件);
url=新url(“http:///test.php");
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
httpURLConnection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
httpURLConnection.setRequestProperty(“Cookie”,标题);
OutputStream output=httpURLConnection.getOutputStream();
PrintWriter writer=新的PrintWriter(新的OutputStreamWriter(输出,“UTF-8”),true;
writer.append(破折号+边界+CRLF);
writer.append(“内容配置:表单数据;名称=\”报告\“文件名=\”报告.pdf \“+CRLF”);
writer.append(“内容类型:application/pdf”+CRLF);
writer.append(CRLF);
writer.flush();
字节[]缓冲区=新字节[1024];
内伦;
而((len=in.read(buffer))!=-1){
输出。写入(缓冲区);
}
output.flush();
writer.append(CRLF);
writer.append(破折号+边界+CRLF);
writer.append(CRLF);
writer.flush();
writer.close();
output.close();
code=httpURLConnection.getResponseCode();
如果(代码==401){
message=“错误。”;
}   
httpURLConnection.disconnect();

$\u POST
中没有任何内容:
Array()
也许可以删除该PHP标记并添加一个Java标记?与其重新创建多部分/表单数据编码,不如让自己更容易,并使用一个好的库,如okhttp。使用MultipartBuilder查看配方:
$\u POST
中没有任何内容:
Array()
也许可以删除该PHP标记并添加一个Java标记?与其重新创建多部分/表单数据编码,不如让自己更容易,并使用一个好的库,如okhttp。使用MultipartBuilder查看配方:但是您的代码不好,也不容易理解。你的代码已经在这个论坛上被找到上百次了。由于协议在某些地方使用空行,您应该清楚地区分它们。例如,notwrite'writer.append(“内容类型:application/pdf”);writer.append(CRLF);'但是writer.append(“内容类型:application/pdf”+CRLF);。保留writer.append(CRLF);对于空行,我已经采纳了您的建议,@greenapps,谢谢。但是如果你感觉到了
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = <my_directory>

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 5M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
String CRLF = "\r\n";
String dashes = "--";
String boundary = "----------j2afevn8a23nfsaj1";

FileInputStream in = new FileInputStream(file);

url = new URL("http://<ip_addr>/test.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");

httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
httpURLConnection.setRequestProperty("Cookie", header);

OutputStream output = httpURLConnection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);

writer.append(dashes + boundary + CRLF);
writer.append("Content-Disposition: form-data; name=\"report\"; filename=\"report.pdf\"" + CRLF);
writer.append("Content-Type: application/pdf" + CRLF);
writer.append(CRLF);

writer.flush();

byte[] buffer = new byte[1024];
int len;

while((len = in.read(buffer)) != -1){
    output.write(buffer);
}

output.flush();
writer.append(CRLF);
writer.append(dashes + boundary + CRLF);
writer.append(CRLF);
writer.flush();
writer.close();
output.close();

code = httpURLConnection.getResponseCode();

if(code == 401){
    message = "Error.";
}   

httpURLConnection.disconnect();