Falmarri
Falmarri

Reputation: 48577

FileNotFound except when getting URL resource with an accent Java

I'm getting a FileNotFoundException when I'm trying to download the file http://tfob.azstarnet.com/images/authors/Alcal%C3%A1_Kathleen_small.jpg. The problem is almost certainly the fact that the URL has an accented character in the string. How can I deal with that?

This is how I'm downloading it.

Log.d(TFOB.TAG, "Image src: " + desc.getString("image"));
productURL = new URL (desc.getString("image").trim());
prod = productURL.openConnection();
is = prod.getInputStream(); // Exception gets thrown here
bis = new BufferedInputStream(is);
bit = BitmapFactory.decodeStream(bis);

This is the stack trace:

Image src: http://tfob.azstarnet.com/images/authors/Alcalá_Kathleen_small.jpg
java.io.FileNotFoundException: http://tfob.azstarnet.com/images/authors/Alcalá_Kathleen_small.jpg
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)

Do I have to escape the accent or something?

Upvotes: 1

Views: 1847

Answers (3)

user6571589
user6571589

Reputation: 1

I have solvede with brutal method:

private Drawable LoadImageFromWebOperations(String strPhotoUrl) {
    try{    
        String lnk = strPhotoUrl;
            lnk = lnk.replaceAll("à","%C3%A0");
            lnk = lnk.replaceAll("è","%C3%A8");
            lnk = lnk.replaceAll("è","%C3%A9");
            lnk = lnk.replaceAll("ì","%C3%AC");
            lnk = lnk.replaceAll("ò","%C3%B2");
            lnk = lnk.replaceAll("ù","%C3%B9");
            Log.i("Tommy", lnk+"\n");
            InputStream is = (InputStream) new URL(lnk).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
            } catch (Exception e) {
               //System.out.println("Exc=" 2e);
                Log.i("Tommy", strPhotoUrl+"\n");
                Log.i("Tommy", e.toString() + "\n\n");
                return null;
            }
}

Upvotes: 0

fider
fider

Reputation: 2036

Solution (in my case):

In case when the server response code is >= HTTP_BAD_REQUEST (greater than 400), method getInputStream() of class HttpURLConnectionImpl throws FileNotFoundException (so you cannot open input stream).

Even if this file exist, your object will not give you input stream, because of server response code is >=400 - change response code on server or use another class to connect.

Fragment of source code: http://www.docjar.com/html/api/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java.html

  867       @Override
  868       public InputStream getInputStream() throws IOException {
  869           if (!doInput) {
  870               throw new ProtocolException(Messages.getString("luni.28")); //$NON-NLS-1$
  871           }
  872
  873           // connect before sending requests
  874           connect();
  875           doRequest();
  876
  ...
  883           if (responseCode >= HTTP_BAD_REQUEST) {
  884               throw new FileNotFoundException(url.toString());
  885           }
  886
  887           return uis;
  888       }

Upvotes: 2

Aaron Saunders
Aaron Saunders

Reputation: 33345

Try this:

URLDecoder

​​​​​​​​​​

​​​​​​​​​​

Upvotes: 0

Related Questions