Java - file path - invalid escape sequence

I upload the file to the destination, providing the path to the file. It works great when the file path is similar to

String filePath = "D:\\location"; 

But when providing the server location, for example

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

throws an invalid escape sequence error.

What is wrong on the way (I have full privileges for the location), and if they are wrong, then how to implement them correctly.

Thanks for the help in advance ////

+8
java filepath
source share
2 answers

This is a compile-time error, so it cannot be related to permissions, etc.

The problem is that you are not avoiding backslashes. You need:

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

Then the contents of the string will be simple

 \\10.0.1.18\downloads\upload 

This is exactly the same as the first line you showed where it is:

 String filePath = "D:\\location"; 

... will actually create a line with the contents:

 D:\location 

For more information on escape sequences in characters and string literals, see section 3.10.6 Java Language Specifications .

+12
source share

use double slash \\ ! This is a special escape pattern. Like \ n or \ r.

+4
source share

All Articles