【发布时间】:2017-06-10 13:27:44
【问题描述】:
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;
}
我的参数变量将包含 url 链接,即“http://ledeveloper.in/ep123BCA.pdf”,我想剪切那个 url..我想要“ep123BCA.pdf”的期望字符串。
但是当我运行这段代码时,子字符串方法总是会给我一个不同的子字符串来解决这个问题。
请提前帮助和thanx。
【问题讨论】:
-
使用split方法
String[] parts= url.split("/");将url分成3或4部分,最后一个是文件名,这是一种方式 -
不适合我,因为它给了我下载文件的相同垃圾名称。 :(