NewBee
NewBee

Reputation: 839

Java - filepath - Invalid escape sequence

I am uploading file to a destination by providing 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////

Upvotes: 9

Views: 40348

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500535

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.

Upvotes: 12

Dariusz
Dariusz

Reputation: 22251

use double slash \\ ! It's a special escape pattern. Like \n or \r.

Upvotes: 5

Related Questions