Reputation: 71
I am currently writing a function to upload files into server. My filename format is typical like this ACTION#USERNAME.TXT, I run into the error
java.lang.StringIndexOutOfBoundsException: length=41; regionStart=38; regionLength=-28
when on this line of code
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
I did some debugging and realised the problem was caused by the character '#'. Taking it out allows the file to be uploaded. The actual file allows for the # character so I don't really understand why would # cause a StringIndexOutOfBoundsException.
Could anyone enlighten me please?
Upvotes: 0
Views: 1012
Reputation: 159165
The #
sign has special meaning in a URL.
The #fragment
part of a URL (see syntax) is never sent from the client to the server. It is a part of the URL that is handled by the client. It can be sent from the server to the client in links and redirects.
To include a #
sign (or any other special character) in the file name, it must be escaped/encoded using %NN
hex codes.
The encoding of #
is %23
, but you really should use a URL encoder.
Upvotes: 2