捕获部分URL以生成FileName

时间:2012-03-19 16:51:14

标签: android webview download filepath

我需要帮助。我的网址的路径是这样的

  

http://www.sample.com/ebooks/27827.epub.images

但我在使用以下代码时将其缩短为

  

27827.epub.images

我需要fileName成为

  

27827.epub

没有.images

String path = url.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);

String fileName = idStr;


InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/" + fileName);

2 个答案:

答案 0 :(得分:2)

要给出完整的答案,您需要定义URL的可能格式。如果你可以假设它总是以" .images"结束,只需剪掉那部分,

fileName = idStr.replace(".images", "");

如果您总是希望切断尾随"。"以及之后的任何事情,你都可以做到,

fileName = idStr;
int idx = fileName.lastIndexOf(".");
if (idx != -1) {
  fileName = fileName.subString(0, idx); 
}

答案 1 :(得分:0)

搜索最后一个'。'在字符串中,然后删除它之后的任何内容