导航:
论坛 -> Web应用开发
斑竹:bodies
作者:
2022/12/26 14:35:55
标题:
如何把这样文件名汉字还原回去: QQ鎴浘20221212002717.png ===> QQ截图20221212002717.png
浏览:1572
加入我的收藏
楼主:
如何把这样文件名汉字还原回去: QQ鎴浘20221212002717.png ===> QQ截图20221212002717.png 我是用TIdMessageDecoderMIME,提取客户端POST上来的文件流,解析过程中,发现文件名汉字乱码,想还原回去. 恳请哪位大哥指点一下, 非常感谢.
----------------------------------------------
...
作者:
2022/12/26 23:20:12
1楼:
hi @akightchen did you tryed some like this? uses idGlobal, // line 1157.... IndyTextEncoding.... IdMessageCoderMIME; ... var LIdMsgDecodeMIME: TIdMessageDecoderMIME; begin ... LIdMsgDecodeMIME.ReadLn(#10, IndyTextEncoding_UTF8); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
2022/12/27 8:39:42
2楼:
I := 0; If ARequestInfo.PostStream <> nil then begin ARequestInfo.PostStream.Position := 0; msgEnd := False; boundary := ExtractHeaderSubItem(ARequestInfo.ContentType, 'boundary',QuoteHTTP); startboundary := '--' + boundary; repeat tmp := ReadLnFromStream(ARequestInfo.PostStream, -1, True); until tmp = startboundary; Decoder := TIdMessageDecoderMIME.Create(nil); try TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary; tsValues := TStringList.Create; try repeat Decoder.SourceStream := ARequestInfo.PostStream; Decoder.FreeSourceStream := False; Decoder.ReadHeader; Inc(I); case Decoder.PartType of mcptAttachment, mcptText: begin ms := TMemoryStream.Create; try ms.Position := 0; newdecoder := Decoder.ReadBody(ms, msgEnd); tmp := Decoder.Headers.Text; fname := Decoder.Filename; //********** here Decoder.Free; Decoder := newdecoder; if Decoder <> nil then TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary; if fname <> '' then begin ms.SaveToFile(fname); end else begin ms.SaveToFile(IntToStr(I) + '.txt'); end; finally ms.Free; end; end; mcptIgnore: Begin FreeAndNil(Decoder); Decoder := TIdMessageDecoderMIME.Create(nil); TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary; End; mcptEOF: begin FreeAndNil(Decoder); msgEnd := True end; end; until (Decoder = nil) or (msgEnd); finally tsValues.Free; end; finally FreeAndNil(Decoder); end; end;
----------------------------------------------
...
作者:
2022/12/27 8:40:05
3楼:
my code is like this.
----------------------------------------------
...
作者:
2022/12/27 21:13:47
4楼:
IdGlobal.pas, line 9647 function ReadLnFromStream(AStream: TStream; var VLine: String; AMaxLineLength: Integer = -1; AByteEncoding: IIdTextEncoding = nil )
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
2022/12/29 9:00:32
5楼:
非常感谢emailx45. 虽然我仍没有成功. 读出来仍是乱码. 我打算换个方法去解决它, 我把文件名放在POST的参数中,这样, 我服务器直接能读到参数.(即: 我暂时放弃直接从Decoder.Filename中得到
----------------------------------------------
...
作者:
2022/12/30 1:23:55
6楼:
嗯, 我把"QQ截图20221212002717.png"存到notepad然后Save as用UTF8保存文件,查看文件二进制内容跟楼主给出"QQ鎴浘20221212002717.png"非常像. 楼主是否想试一下直接把收到的字符串用UTF8Decode, 也许就是那样.
----------------------------------------------
-
作者:
2023/1/1 20:40:56
7楼:
Easy.... Showmessage(TEncoding.UTF8.GetString(TEncoding.ANSI.GetBytes('QQ鎴浘20221212002717.png')));
----------------------------------------------
-
作者:
2023/1/1 20:42:27
8楼:
原因在于,你把对方传给你的UTF8字符,错当GB2312给解析了。。。。
----------------------------------------------
-
作者:
2023/1/2 15:25:09
9楼:
我就是按照这个修改的 https://stackoverflow.com/questions/27257577/indy-mime-decoding-of-multipart-form-data-requests-returns-trailing-cr-lf
----------------------------------------------
-
作者:
2023/1/2 15:56:16
10楼:
TO teclick (nelson): 你发的这个链接, 我也曾搜索过, 但它并没有涉及 文件名转码问题.(可能作者是英文, 他不会遇到这种问题) 你这个例子中, 也只是直接使用: // write stream to upload folder UploadFile := GetUploadFolder + VDecoder.Filename; LMStream.SaveToFile(UploadFile); 即: 直接使用VDecoder.Filename. (一直也存在中文问题)
----------------------------------------------
...
作者:
2023/1/2 16:06:11
11楼:
我又监测了一下 F7进入代码: Decoder.ReadHeader; 发现它读出的数据是这样: 'Content-Disposition: form-data; name="data"; filename="QQ□□□□□□20221212002717.png"' 一个汉字由3个框组成.
----------------------------------------------
...
作者:
2023/1/2 16:06:46
11楼:
就一句代码: IdGlobal.GIdDefaultTextEncoding := encUTF8; 来源于indy作者的回答: https://stackoverflow.com/questions/70171786 注意看Remy Lebeau的回复那一大段
----------------------------------------------
delphi 是兴趣,和工作无关,即使它倒闭。又不靠它 delphi 吃饭,怕甚?
作者:
2023/1/2 16:10:48
12楼:
sail2000 (小帆工作室), 感谢您的指点, 加上你这一句, 就行了!!!!!
----------------------------------------------
...
作者:
2023/1/2 16:12:55
13楼:
最后,我总结一下代码: I := 0; If ARequestInfo.PostStream <> nil then begin ARequestInfo.PostStream.Position := 0; msgEnd := False; boundary := ExtractHeaderSubItem(ARequestInfo.ContentType, 'boundary',QuoteHTTP); startboundary := '--' + boundary; repeat tmp := ReadLnFromStream(ARequestInfo.PostStream, -1, True); until tmp = startboundary; IdGlobal.GIdDefaultTextEncoding := encUTF8; //加上sail2000 (小帆工作室),这一句,就行了Decoder.Filename就能正确显示汉字了 Decoder := TIdMessageDecoderMIME.Create(nil); try TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary; tsValues := TStringList.Create; try repeat Decoder.SourceStream := ARequestInfo.PostStream; Decoder.FreeSourceStream := False; Decoder.ReadHeader; Inc(I); case Decoder.PartType of mcptAttachment, mcptText: begin ms := TMemoryStream.Create; try ms.Position := 0; newdecoder := Decoder.ReadBody(ms, msgEnd); tmp := Decoder.Headers.Text; fname := Decoder.Filename; //********** here Decoder.Free; Decoder := newdecoder; if Decoder <> nil then TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary; if fname <> '' then begin ms.SaveToFile(fname); end else begin ms.SaveToFile(IntToStr(I) + '.txt'); end; finally ms.Free; end; end; mcptIgnore: Begin FreeAndNil(Decoder); Decoder := TIdMessageDecoderMIME.Create(nil); TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary; End; mcptEOF: begin FreeAndNil(Decoder); msgEnd := True end; end; until (Decoder = nil) or (msgEnd); finally tsValues.Free; end; finally FreeAndNil(Decoder); end; end;
----------------------------------------------
...
作者:
2023/1/2 16:15:47
14楼:
感谢"sail2000 (小帆工作室)"您的金言! 也感谢其它参与的朋友: emailx45,newbuyer, crystalmoon, teclick 完美结贴!!!
----------------------------------------------
...