gpt4 book ai didi

java - 通过 Java 上传 $_FILES 数组为空

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:06 24 4
gpt4 key购买 nike

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

我验证了文件确实正在上传(wireshark 转储):

No.     Time        Source                Destination           Protocol Length Info
16 2.493417 <ip_addr> <ip_addr> HTTP 877 POST /test.php HTTP/1.1

Frame 16: 877 bytes on wire (7016 bits), 877 bytes captured (7016 bits)
Ethernet II, Src: Cisco_5a:0b:41 (<mac_address>), Dst: <mac_address> (<mac_address>)
Internet Protocol Version 4, Src: <ip_addr> (<ip_addr>), Dst: <ip_addr> (<ip_addr>)
Transmission Control Protocol, Src Port: 52506 (52506), Dst Port: http (80), Seq: 1958, Ack: 461, Len: 811
[3 Reassembled TCP Segments (2485 bytes): #13(306), #14(1368), #16(811)]
Hypertext Transfer Protocol
POST /test.php HTTP/1.1\r\n
Cookie: PHPSESSID=<sessionid>\r\n
Connection: Keep-Alive\r\n
Content-Type: multipart/form;boundary=----------j2afevn8a23nfsaj1\r\n
User-Agent: <user_agent>\r\n
Host: <ip_addr>\r\n
Accept-Encoding: gzip\r\n
Content-Length: 2179\r\n
\r\n
[Full request URI: http://<ip_addr>/test.php]
[HTTP request 2/6]
[Prev request in frame: 9]
[Response in frame: 80]
MIME Multipart Media Encapsulation, Type: multipart/form, Boundary: "----------j2afevn8a23nfsaj1"
[Type: multipart/form]
Data (2179 bytes)

0000 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 6a 32 61 66 65 76 ----------j2afev
0010 6e 38 61 32 33 6e 66 73 61 6a 31 0d 0a 43 6f 6e n8a23nfsaj1..Con
0020 74 65 6e 74 2d 64 69 73 70 6f 73 69 74 69 6f 6e tent-disposition
0030 3a 20 66 6f 72 6d 2d 64 61 74 61 3b 20 6e 61 6d : form-data; nam
0040 65 3d 22 72 65 70 6f 72 74 22 3b 66 69 6c 65 6e e="report";filen
0050 61 6d 65 3d 22 72 65 70 6f 72 74 2e 70 64 66 22 ame="report.pdf"
0060 0d 0a 25 50 44 46 2d 31 2e 34 0a 25 e2 e3 cf d3 ..%PDF-1.4.%....
0070 0a 32 20 30 20 6f 62 6a 0a 3c 3c 2f 46 69 6c 74 .2 0 obj.<</Filt
0080 65 72 2f 46 6c 61 74 65 44 65 63 6f 64 65 2f 4c er/FlateDecode/L
0090 65 6e 67 74 68 20 33 38 38 3e 3e 73 74 72 65 61 ength 388>>strea
00a0 6d 0a 78 9c 95 53 5b 6f 82 30 18 7d ef af f8 1e m.x..S[o.0.}....
00b0 dd 83 d8 96 3b 6f bb b1 65 89 66 6e 2c 3e 33 29 ....;o..e.fn,>3)
00c0 82 13 ab 05 67 f6 ef 57 44 17 47 29 d1 07 02 e1 ....g..WD.G)....
00d0 5c 38 39 3d 6c d1 5d 84 4c 07 3c ec 40 94 20 0c \89=l.].L.<.@. .
00e0 43 e2 d5 0f a3 90 00 a1 10 a5 68 30 b9 9d 85 30 C.........h0...0
00f0 e6 eb bc e2 02 de d8 86 8b ea 26 5a 4a 2a fe a7 ..........&ZJ*..
0100 18 84 b1 28 02 18 ff a4 f2 de 49 98 b1 b8 ca 98 ...(......I.....
0110 00 91 97 5f 01 d8 b8 93 34 d9 15 9f 92 c3 53 48 ..._....4.....SH
0120 73 b6 4a ca 00 68 27 6f d8 f9 36 ca 18 a4 7c b5 s.J..h'o..6...|.
0130 e2 fb 7c bd 38 3a 40 16 7f 33 10 2c 9e 67 2c 81 ..|.8:@..3.,.g,.
0140 f9 ae e2 bb 2a b8 c4 d3 f2 74 95 5c f1 f1 58 30 ....*....t.\..X0
0150 d8 08 be 64 f3 4a 7e bd e2 4d 90 6b 62 90 c3 c9 ...d.J~..M.kb...
0160 3c 46 68 8a b6 87 8b c2 8b 7c ff 84 b0 61 c3 1e <Fh......|...a..
0170 79 9e 61 82 e3 58 40 4c df b0 5c 90 74 c1 d0 fb y.a..X@L..\.t...
0180 11 a5 d4 35 5c 57 8f 9b 8e 6b d0 1e 7d e3 2e 9b ...5\W...k..}...
0190 e8 75 d7 e1 27 77 1d de b8 9b b4 df 5d 87 9f dc .u..'w......]...
01a0 55 7c 5a af 9a 40 dd 21 01 1f 1f 0a 92 2d 16 e7 U|Z..@.!.....-..
01b0 67 18 d6 e7 03 eb b8 60 75 dd b2 de 33 09 a5 7e g......`u...3..~
01c0 d3 5a 5b f4 10 57 1d 74 d3 f1 9b 12 db f4 67 b9 .Z[..W.t......g.
01d0 78 f8 90 7f 4e a9 8a 9a 58 36 6e c7 4a 85 d0 07 x...N...X6n.J...
01e0 52 e8 d8 1d 51 32 22 b6 3e 94 2a d1 65 31 ad 16 R...Q2".>.*.e1..
01f0 f3 3e 5d 94 a9 3e 8c c2 af c3 d0 de 30 aa e4 c8 .>]..>......0...
0200 d5 2e db 76 fa d7 a1 c5 8f eb e8 c0 d5 75 d8 4e ...v.........u.N
0210 3b d7 05 eb 50 45 af 7f ff 79 d2 b7 13 55 a8 ec ;...PE...y...U..
0220 e4 17 d4 6c 42 dd 0a 65 6e 64 73 74 72 65 61 6d ...lB..endstream
0230 0a 65 6e 64 6f 62 6a 0a 34 20 30 20 6f 62 6a 0a .endobj.4 0 obj.
0240 3c 3c 2f 52 65 73 6f 75 72 63 65 73 3c 3c 2f 46 <</Resources<</F
0250 6f 6e 74 3c 3c 2f 46 31 20 31 20 30 20 52 3e 3e ont<</F1 1 0 R>>
0260 3e 3e 2f 43 6f 6e 74 65 6e 74 73 20 32 20 30 20 >>/Contents 2 0
0270 52 2f 4d 65 64 69 61 42 6f 78 5b 30 20 30 20 35 R/MediaBox[0 0 5
0280 39 35 20 38 34 32 5d 2f 50 61 72 65 6e 74 20 33 95 842]/Parent 3
0290 20 30 20 52 2f 54 79 70 65 2f 50 61 67 65 3e 3e 0 R/Type/Page>>
02a0 0a 65 6e 64 6f 62 6a 0a 31 20 30 20 6f 62 6a 0a .endobj.1 0 obj.
02b0 3c 3c 2f 42 61 73 65 46 6f 6e 74 2f 48 65 6c 76 <</BaseFont/Helv
02c0 65 74 69 63 61 2f 54 79 70 65 2f 46 6f 6e 74 2f etica/Type/Font/
02d0 45 6e 63 6f 64 69 6e 67 2f 57 69 6e 41 6e 73 69 Encoding/WinAnsi
02e0 45 6e 63 6f 64 69 6e 67 2f 53 75 62 74 79 70 65 Encoding/Subtype
02f0 2f 54 79 70 65 31 3e 3e 0a 65 6e 64 6f 62 6a 0a /Type1>>.endobj.
0300 33 20 30 20 6f 62 6a 0a 3c 3c 2f 4b 69 64 73 5b 3 0 obj.<</Kids[
0310 34 20 30 20 52 5d 2f 43 6f 75 6e 74 20 31 2f 54 4 0 R]/Count 1/T
0320 79 70 65 2f 50 61 67 65 73 3e 3e 0a 65 6e 64 6f ype/Pages>>.endo
0330 62 6a 0a 35 20 30 20 6f 62 6a 0a 3c 3c 2f 50 61 bj.5 0 obj.<</Pa
0340 67 65 73 20 33 20 30 20 52 2f 54 79 70 65 2f 43 ges 3 0 R/Type/C
0350 61 74 61 6c 6f 67 3e 3e 0a 65 6e 64 6f 62 6a 0a atalog>>.endobj.
0360 36 20 30 20 6f 62 6a 0a 3c 3c 2f 4d 6f 64 44 61 6 0 obj.<</ModDa
0370 74 65 28 44 3a 32 30 31 35 30 37 32 33 31 31 32 te(D:20150723112
0380 37 33 34 2d 30 35 27 30 30 27 29 2f 43 72 65 61 734-05'00')/Crea
0390 74 69 6f 6e 44 61 74 65 28 44 3a 32 30 31 35 30 tionDate(D:20150
03a0 37 32 33 31 31 32 37 33 34 2d 30 35 27 30 30 27 723112734-05'00'
03b0 29 2f 54 69 74 6c 65 28 4e 41 57 46 20 4d 6f 6e )/Title(********
03c0 69 74 6f 72 20 52 65 70 6f 72 74 29 2f 50 72 6f ***********)/Pro
03d0 64 75 63 65 72 28 69 54 65 78 74 ae 20 35 2e 35 ducer(iText. 5.5
03e0 2e 34 20 a9 32 30 30 30 2d 32 30 31 34 20 69 54 .4 .2000-2014 iT
03f0 65 78 74 20 47 72 6f 75 70 20 4e 56 20 5c 28 41 ext Group NV \(A
0400 47 50 4c 2d 76 65 72 73 69 6f 6e 5c 29 29 3e 3e GPL-version\))>>
0410 0a 65 6e 64 6f 62 6a 0a 78 72 65 66 0a 30 20 37 .endobj.xref.0 7
0420 0a 30 30 30 30 30 30 30 30 30 30 20 36 35 35 33 .0000000000 6553
0430 35 20 66 20 0a 30 30 30 30 30 30 30 35 38 32 20 5 f .0000000582
0440 30 30 30 30 30 20 6e 20 0a 30 30 30 30 30 30 30 00000 n .0000000
0450 30 31 35 20 30 30 30 30 30 20 6e 20 0a 30 30 30 015 00000 n .000
0460 30 30 30 30 36 37 30 20 30 30 30 30 30 20 6e 20 0000670 00000 n
0470 0a 30 30 30 30 30 30 30 34 37 30 20 30 30 30 30 .0000000470 0000
0480 30 20 6e 20 0a 30 30 30 30 30 30 30 37 32 31 20 0 n .0000000721
0490 30 30 30 30 30 20 6e 20 0a 30 30 30 30 30 30 30 00000 n .0000000
04a0 37 36 36 20 30 30 30 30 30 20 6e 20 0a 74 72 61 766 00000 n .tra
04b0 69 6c 65 72 0a 3c 3c 2f 52 6f 6f 74 20 35 20 30 iler.<</Root 5 0
04c0 20 52 2f 53 69 7a 65 20 37 2f 49 44 20 5b 3c 65 R/Size 7/ID [<e
04d0 34 62 35 61 33 39 33 39 31 65 35 64 31 33 35 33 4b5a39391e5d1353
04e0 37 34 65 32 66 37 35 64 65 65 30 65 38 61 34 3e 74e2f75dee0e8a4>
04f0 3c 65 34 62 35 61 33 39 33 39 31 65 35 64 31 33 <e4b5a39391e5d13
0500 35 33 37 34 65 32 66 37 35 64 65 65 30 65 38 61 5374e2f75dee0e8a
0510 34 3e 5d 2f 49 6e 66 6f 20 36 20 30 20 52 3e 3e 4>]/Info 6 0 R>>
0520 0a 25 69 54 65 78 74 2d 35 2e 35 2e 34 0a 73 74 .%iText-5.5.4.st
0530 61 72 74 78 72 65 66 0a 39 35 30 0a 25 25 45 4f artxref.950.%%EO
0540 46 0a e2 bb 2a b8 c4 d3 f2 74 95 5c f1 f1 58 30 F...*....t.\..X0
0550 d8 08 be 64 f3 4a 7e bd e2 4d 90 6b 62 90 c3 c9 ...d.J~..M.kb...
0560 3c 46 68 8a b6 87 8b c2 8b 7c ff 84 b0 61 c3 1e <Fh......|...a..
0570 79 9e 61 82 e3 58 40 4c df b0 5c 90 74 c1 d0 fb y.a..X@L..\.t...
0580 11 a5 d4 35 5c 57 8f 9b 8e 6b d0 1e 7d e3 2e 9b ...5\W...k..}...
0590 e8 75 d7 e1 27 77 1d de b8 9b b4 df 5d 87 9f dc .u..'w......]...
05a0 55 7c 5a af 9a 40 dd 21 01 1f 1f 0a 92 2d 16 e7 U|Z..@.!.....-..
05b0 67 18 d6 e7 03 eb b8 60 75 dd b2 de 33 09 a5 7e g......`u...3..~
05c0 d3 5a 5b f4 10 57 1d 74 d3 f1 9b 12 db f4 67 b9 .Z[..W.t......g.
05d0 78 f8 90 7f 4e a9 8a 9a 58 36 6e c7 4a 85 d0 07 x...N...X6n.J...
05e0 52 e8 d8 1d 51 32 22 b6 3e 94 2a d1 65 31 ad 16 R...Q2".>.*.e1..
05f0 f3 3e 5d 94 a9 3e 8c c2 af c3 d0 de 30 aa e4 c8 .>]..>......0...
0600 d5 2e db 76 fa d7 a1 c5 8f eb e8 c0 d5 75 d8 4e ...v.........u.N
0610 3b d7 05 eb 50 45 af 7f ff 79 d2 b7 13 55 a8 ec ;...PE...y...U..
0620 e4 17 d4 6c 42 dd 0a 65 6e 64 73 74 72 65 61 6d ...lB..endstream
0630 0a 65 6e 64 6f 62 6a 0a 34 20 30 20 6f 62 6a 0a .endobj.4 0 obj.
0640 3c 3c 2f 52 65 73 6f 75 72 63 65 73 3c 3c 2f 46 <</Resources<</F
0650 6f 6e 74 3c 3c 2f 46 31 20 31 20 30 20 52 3e 3e ont<</F1 1 0 R>>
0660 3e 3e 2f 43 6f 6e 74 65 6e 74 73 20 32 20 30 20 >>/Contents 2 0
0670 52 2f 4d 65 64 69 61 42 6f 78 5b 30 20 30 20 35 R/MediaBox[0 0 5
0680 39 35 20 38 34 32 5d 2f 50 61 72 65 6e 74 20 33 95 842]/Parent 3
0690 20 30 20 52 2f 54 79 70 65 2f 50 61 67 65 3e 3e 0 R/Type/Page>>
06a0 0a 65 6e 64 6f 62 6a 0a 31 20 30 20 6f 62 6a 0a .endobj.1 0 obj.
06b0 3c 3c 2f 42 61 73 65 46 6f 6e 74 2f 48 65 6c 76 <</BaseFont/Helv
06c0 65 74 69 63 61 2f 54 79 70 65 2f 46 6f 6e 74 2f etica/Type/Font/
06d0 45 6e 63 6f 64 69 6e 67 2f 57 69 6e 41 6e 73 69 Encoding/WinAnsi
06e0 45 6e 63 6f 64 69 6e 67 2f 53 75 62 74 79 70 65 Encoding/Subtype
06f0 2f 54 79 70 65 31 3e 3e 0a 65 6e 64 6f 62 6a 0a /Type1>>.endobj.
0700 33 20 30 20 6f 62 6a 0a 3c 3c 2f 4b 69 64 73 5b 3 0 obj.<</Kids[
0710 34 20 30 20 52 5d 2f 43 6f 75 6e 74 20 31 2f 54 4 0 R]/Count 1/T
0720 79 70 65 2f 50 61 67 65 73 3e 3e 0a 65 6e 64 6f ype/Pages>>.endo
0730 62 6a 0a 35 20 30 20 6f 62 6a 0a 3c 3c 2f 50 61 bj.5 0 obj.<</Pa
0740 67 65 73 20 33 20 30 20 52 2f 54 79 70 65 2f 43 ges 3 0 R/Type/C
0750 61 74 61 6c 6f 67 3e 3e 0a 65 6e 64 6f 62 6a 0a atalog>>.endobj.
0760 36 20 30 20 6f 62 6a 0a 3c 3c 2f 4d 6f 64 44 61 6 0 obj.<</ModDa
0770 74 65 28 44 3a 32 30 31 35 30 37 32 33 31 31 32 te(D:20150723112
0780 37 33 34 2d 30 35 27 30 30 27 29 2f 43 72 65 61 734-05'00')/Crea
0790 74 69 6f 6e 44 61 74 65 28 44 3a 32 30 31 35 30 tionDate(D:20150
07a0 37 32 33 31 31 32 37 33 34 2d 30 35 27 30 30 27 723112734-05'00'
07b0 29 2f 54 69 74 6c 65 28 4e 41 57 46 20 4d 6f 6e )/Title(********
07c0 69 74 6f 72 20 52 65 70 6f 72 74 29 2f 50 72 6f ***********)/Pro
07d0 64 75 63 65 72 28 69 54 65 78 74 ae 20 35 2e 35 ducer(iText. 5.5
07e0 2e 34 20 a9 32 30 30 30 2d 32 30 31 34 20 69 54 .4 .2000-2014 iT
07f0 65 78 74 20 47 72 6f 75 70 20 4e 56 20 5c 28 41 ext Group NV \(A
0800 47 50 4c 2d 76 65 72 73 69 6f 6e 5c 29 29 3e 3e GPL-version\))>>
0810 0a 65 6e 64 6f 62 6a 0a 78 72 65 66 0a 30 20 37 .endobj.xref.0 7
0820 0a 30 30 30 30 30 30 30 30 30 30 20 36 35 35 33 .0000000000 6553
0830 35 20 66 20 0a 30 30 30 30 30 30 30 35 38 32 20 5 f .0000000582
0840 30 30 30 30 30 20 6e 20 0a 30 30 30 30 30 30 30 00000 n .0000000
0850 30 31 35 20 30 30 30 30 30 20 6e 20 0a 30 30 30 015 00000 n .000
0860 30 30 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 6a 32 00..----------j2
0870 61 66 65 76 6e 38 61 32 33 6e 66 73 61 6a 31 0d afevn8a23nfsaj1.
0880 0a 0d 0a ...
Data: 2d2d2d2d2d2d2d2d2d2d6a32616665766e386132336e6673...
[Length: 2179]

这是我的java代码:

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

FileInputStream in = new FileInputStream(file);

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

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

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

writer.append(boundary);
writer.append(CRLF);
writer.append("Content-disposition: form-data; name=\"report\";filename=\"report.pdf\"");
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(boundary);
writer.append(CRLF);
writer.append(CRLF);
writer.flush();
writer.close();
output.close();

code = httpURLConnection.getResponseCode();

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

httpURLConnection.disconnect();

最后,这是 PHP 相关代码:

if ($_FILES['report']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['report']['tmp_name'])){

echo "File is uploaded!";
}

else{

$text = print_r($_FILES, true);
echo "File is not uploaded! " . $text;
}

这是 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

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

最佳答案

好的,我修好了。每当涉及 HTTP header 时,您都需要小心发送精确的字符串。例如,我使用了 Content-disposition 而不是 Content-Disposition,并且在每次使用 boundary 字符串之前没有添加双破折号。我还犯了一个明显的错误,写的是 multipart/form 而不是 multipart/form-data 最后,我包含了一个 Content-Type header ,并在实际数据之前添加了两个返回。比较代码如下:

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();

关于java - 通过 Java 上传 $_FILES 数组为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com