最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】jsp中将反斜杠替换为斜杠

JSP crifan 4637浏览 0评论

【问题】

KindEditor中上传图片后,在Chrome中正常显示:

jsp page show normally

但是在Firefox中却无法显示:

firefox image not show normally

经过调试发现,原来是URL:

http://127.0.0.1:8080/xxxxx\lib\kindeditor/attached/image/20150721/20150721155122_136.jpg

的问题。

内部相关部分代码是:

//文件保存目录路径
String getRequestURI = request.getRequestURI();
out.println("getRequestURI=" + getRequestURI);
// getRequestURI=/xxxxxx/lib/kindeditor/jsp/upload_json.jsp
StringBuffer getRequestURL = request.getRequestURL();
out.println("getRequestURL=" + getRequestURL);
// getRequestURL=http://127.0.0.1:8080/xxxxxx/lib/kindeditor/jsp/upload_json.jsp
String getContextPath = request.getContextPath();
out.println("getContextPath=" + getContextPath);
// getContextPath=/xxxxxx
String getServletPath = request.getServletPath();
out.println("getServletPath=" + getServletPath);
// getServletPath=/lib/kindeditor/jsp/upload_json.jsp
String servlerParentPath = new File(getServletPath).getParent();
out.println("servlerParentPath=" + servlerParentPath);
// servlerParentPath=\lib\kindeditor\jsp
String servlerParentParentPath = new File(servlerParentPath).getParent();
out.println("servlerParentParentPath=" + servlerParentParentPath);
// servlerParentParentPath=\lib\kindeditor
String kindeditorRootRelativePath = getContextPath + servlerParentParentPath;
out.println("kindeditorRootRelativePath=" + kindeditorRootRelativePath);
// kindeditorRootRelativePath=/xxxxxx\lib\kindeditor

String serveletRealPath = application.getRealPath(getServletPath);
out.println("serveletRealPath=" + serveletRealPath);
// follow line single slash replace with two slash, otherwise will error: Invalid unicode
// serveletRealPath=E:\\dev_install\\apache-tomcat\\apache-tomcat-7.0.25\\webapps\\xxxxxx\\lib\\kindeditor\\jsp\\upload_json.jsp
String serveletRealPathParent = new File(serveletRealPath).getParent();
out.println("serveletRealPathParent=" + serveletRealPathParent);
// serveletRealPathParent=E:\dev_install\apache-tomcat\apache-tomcat-7.0.25\webapps\xxxxxx\lib\kindeditor\jsp
String serveletRealPathParentParent = new File(serveletRealPathParent).getParent();
out.println("serveletRealPathParentParent=" + serveletRealPathParentParent);
// serveletRealPathParentParent=E:\dev_install\apache-tomcat\apache-tomcat-7.0.25\webapps\xxxxxx\lib\kindeditor
String appRootPath = serveletRealPathParentParent;
out.println("appRootPath=" + appRootPath);
// appRootPath=E:\dev_install\apache-tomcat\apache-tomcat-7.0.25\webapps\xxxxxx\lib\kindeditor

String savePath = appRootPath + "/attached/";
// String savePath = "E:\\dev_install\\apache-tomcat\\apache-tomcat-7.0.25\\webapps\\xxxxxx\\lib\\kindeditor\\attached";

//文件保存目录URL
// String saveUrl  = request.getContextPath() + "/attached/";
// String saveUrl  = appRootPath + "/attached/";
String saveUrl  = kindeditorRootRelativePath + "/attached/";

中的:

kindeditorRootRelativePath=/xxxxxx\lib\kindeditor

对应的,该URL在浏览器中无法显示:

image url can not show ok

http://127.0.0.1:8080/xxxxxx%5Clib%5Ckindeditor/attached/image/20150721/20150721155122_136.jpg

所以问题转换为:

如何在jsp中,将反斜杠\替换为斜杠:

这样就可以把:

http://127.0.0.1:8080/xxxxxx\lib\kindeditor/attached/image/20150721/20150721155122_136.jpg

换成:

http://127.0.0.1:8080/xxxxxx/lib/kindeditor/attached/image/20150721/20150721155122_136.jpg

然后图片就可以正常显示了。

因为在路径中的斜杠,不论对于Windows还是Linux,都是可以识别的。

 

【折腾过程】

1.搜:

jsp backslash to slash

参考:

Escaping the backslash character in JSP | Oracle Community

java – Exception while replacing backslashes to slashes – Stack Overflow

很明显,的确看起来是简单的:

.replace("\\", "/")

就可以了。

但是,感觉还是有更好的办法才对。

2.How to replace the backslash with the forward slash using java – Toolbox for IT Groups

java – How can I escape both quotation mark and backslash in jsp? – Stack Overflow

提到了:

StringEscapeUtils (Commons Lang 2.6 API)

发现里面的:

unescapeHtml

貌似是转义html的。

而不是我此处的url中,将反斜杠换成斜杠的。

3.所以还是简单的去:

String savePath = appRootPath + "/attached/";
// String savePath = "E:\\dev_install\\apache-tomcat\\apache-tomcat-7.0.25\\webapps\\xxxxxx\\lib\\kindeditor\\attached";
savePath = savePath.replaceAll("\\","/");
//文件保存目录URL
// String saveUrl  = request.getContextPath() + "/attached/";
// String saveUrl  = appRootPath + "/attached/";
String saveUrl  = kindeditorRootRelativePath + "/attached/";
saveUrl = saveUrl.replaceAll("\\","/"); 

再去试试结果:

结果出错了。

4.再去搜:

jsp replaceAll

String replaceAll

参考:

java中String字符串的替换函数:replace与replaceAll的区别 – coolwzjcool的专栏 – 博客频道 – CSDN.NET

String (Java Platform SE 7 )

  • replaceAll
    public String replaceAll(String regex,
                    String replacement)

    Replaces each substring of this string that matches the given regular expression with the given replacement.

    An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

    Pattern.compile(regex).matcher(str).replaceAll(repl)

    Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

    Parameters:
    regex – the regular expression to which this string is to be matched
    replacement – the string to be substituted for each match
    Returns:
    The resulting String
    Throws:
    PatternSyntaxException – if the regular expression’s syntax is invalid
    Since:
    1.4
    See Also:
    Pattern
  • replace
    public String replace(CharSequence target,
                 CharSequence replacement)

    Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

    Parameters:
    target – The sequence of char values to be replaced
    replacement – The replacement sequence of char values
    Returns:
    The resulting string
    Throws:
    NullPointerException – if target or replacement is null.
    Since:
    1.5

才知道:

replaceAll是正则的替换 ->反斜杠就有特别的转义的功能了。

replace才是普通的字符串的替换->反斜杠没有特殊的功能,就只是反斜杠而已,所以可以正常替换

所以改为:

savePath = savePath.replace("\\", "/");

saveUrl = saveUrl.replace("\\", "/");

结果是:

可以了。

replace backslash then image show ok

【总结】

Jsp中,将反斜杠替换为斜杠,直接用:

yourStrValue = yourStrValue.replace("\\", "/");

即可。

注意:

不要把replace写成(用正则的方式去替换的)replaceAll了。

转载请注明:在路上 » 【已解决】jsp中将反斜杠替换为斜杠

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.182 seconds, using 22.13MB memory