java - 如何在java中使用带有字符串数组的SubString方法

标签 java android

class DownloadFileAsync extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            int count;
            String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" ;


            String[] fileName = params;
            String a = fileName.toString();
            String b = a.substring(20,25);
            destination+=b;


            try {

                URL url = new URL(params[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lengthofFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Length of file: " + lengthofFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(destination);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lengthofFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            return null;
        }

我的参数变量将包含“http://ledeveloper.in/ep123BCA.pdf”的url链接,我想剪切该url..我想要的“ep123BCA.pdf”的愿望字符串。

但是当我运行这段代码时,子字符串方法总是会给我一个不同的子字符串,如何解决这个问题。

请提前提供帮助并致谢。

最佳答案

使用lastIndexOf从URL中获取文件名:

String b = a.substring(a.lastIndexOf('/') + 1);

关于java - 如何在java中使用带有字符串数组的SubString方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41850364/

相关文章:

java - 如何在 Eclipse 中查看特定类的源代码?

android - Firebase 云消息系统 UI 已停止

android - 动态启用或禁用小部件不起作用

android - 数据绑定(bind) : Choose one of string resources depends on condition

java - 在 JVM 之间共享类

java - 尝试让 BufferedWriter 将文本区域保存到 txt 文件

java - map 缩小作业以继续播放

android - 如何更改 NsdManager 使用的 android 设备名称?

java - 错误膨胀类 com.android.internal.widget.ActionBarContainer

java - 如何使用gradle创建Dropwizard项目?