Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - filepath - Invalid escape sequence

Tags:

java

filepath

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////

like image 965
NewBee Avatar asked Apr 19 '13 06:04

NewBee


2 Answers

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.

like image 188
Jon Skeet Avatar answered Sep 25 '22 10:09

Jon Skeet


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

like image 43
Dariusz Avatar answered Sep 21 '22 10:09

Dariusz