首页 > 编程知识 正文

javaurlencode转码,java encode

时间:2023-05-04 14:38:06 阅读:40168 作者:2666

今天实现的功能之一是在页面上将文件上传到后台。 文件为txt格式,每行为url。 在后台导入后,对路径中的中文进行url编码

我的代码如下:

patternchinesepattern=pattern.com pile (((() ) ) ) fa5 ) ) );

多文件文件=null;

try {

booleanismultipart=servlet file upload.ismultipartcontent (request;

if (is多点) {

multiparthttpservletrequestmultipartrequest=(multiparthttpservletrequest ) request;

file=multipart request.getfile (' file );

字符串行=null;

SeturlOrNameSet=new HashSet (;

bufferedreaderreader=newbufferedreader (newinputstreamreader (file.get inputstream )、' GB18030 ' ); //这里实际上也是默认情况下用GB18030编码读取的

while((line=reader.readline ) )!=空) {

if(stringutils.isnotblank(line ) ) {

line=line.trim (;

matcher matcher=Chinese pattern.matcher (line );

wile(Matcher.find ) () () ) ) ) )。

line=line.replace all (matcher.group ),urlencoder.encode (matcher.group ) );

}

urlornameset.add(line;

}

}

}

}catch(exceptione ) {

logger.error (batchimportfail : ),e );

}

但是,发现了奇怪的问题。 编码后的路径错误,完全无法访问。 见鬼。 所以我做了单元测试:

@Test

publicvoidtesturl throws exception { }

string URL=' https://baike.Baidu.com/item /乒乓球台/2565939 ';

patternchinesepattern=pattern.com pile (((() ) ) ) fa5 ) ) );

matcher matcher=Chinese pattern.matcher (URL;

wile(Matcher.find ) () () ) ) ) )。

URL=URL.replace all (matcher.group ),urlencoder.encode (matcher.group ) ) )

}

system.out.println(URL;

}

代码与上面的类似,但使用此方法编码的路径可以成功访问。 而且,确实,上述编码路径与这里的单元测试编码的路径不同

可恶,代码是一样的,为什么结果不一样? 首先考虑的是编码问题,但这两个位置的url都是UTF-8,所以url的编码一致。 问题不在网址上

最后,我想可能是URLEncoder.encode的过程中出现了问题。 因此,如果你先去源代码看看,你会发现以下内容。

@Deprecated

publicstaticstringencode{

字符串str=null;

try {

str=encode(s,dfltEncName;

} catch (unsupportedencodingexceptione ) {

//thesystemshouldalwayshavetheplatformdefault

}

返回str;

}

可见这是一种过时的方法。 当然,这不重要。 重要的是,这里有默认代码dfltEncName。 于是,我看了debug,单元测试的位置,这里的默认代码是UTF-8,但在我的项目中这里竟然是GBK。 因此,我更改了以下代码。

line=line.replace all (matcher.group )、urlencoder.encode (matcher.group )、' UTF-8 ' ) )

问题终于解决了

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。