简体   繁体   English

Java - filepath - 无效的转义序列

[英]Java - filepath - Invalid escape sequence

I am uploading file to a destination by providing filepath. 我通过提供filepath将文件上传到目的地。 It works fine when file path is like 它在文件路径类似时工作正常

String filePath = "D:\\location";

But while providing a server location like 但是提供像服务器一样的位置

String filePath = request.getRealPath("\\10.0.1.18\downloads\upload");

produce error of invalid escape sequence. 产生无效转义序列的错误。

Whats wrong in the path ( i have full priveledges to the location) and if wrong how too impliment it correctly. 在路径上有什么不对(我有充分的位置),如果错误,它是如何正确地恭维它。

Thanks for help in advance//// 提前感谢您的帮助////

It's a compile-time error, so it can't be to do with permissions etc. 这是一个编译时错误,所以它不能与权限等有关。

The problem is that you're not escaping the backslashes. 问题是你没有逃避反斜杠。 You need: 你需要:

String filePath = request.getRealPath("\\\\10.0.1.18\\downloads\\upload");

Then the contents of the string will be just 然后字符串的内容将是正确的

\\10.0.1.18\downloads\upload

This is exactly the same as in the first line you showed, where this: 这与您显示的第一行完全相同,其中:

String filePath = "D:\\location";

... will actually create a string with contents of: ...实际上会创建一个内容为的字符串:

D:\location

See section 3.10.6 of the Java Language Specification for more details of escape sequences within character and string literals. 有关字符和字符串文字中转义序列的更多详细信息,请参阅Java语言规范的第3.10.6节

use double slash \\\\ ! 使用双斜线\\\\ It's a special escape pattern. 这是一种特殊的逃脱模式。 Like \\n or \\r. 喜欢\\ n或\\ r。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM