如何在java中使用带字符串数组的子字符串方法 class DownloadFileAsync扩展了AsyncTask{ @凌驾 受保护的字符串doInBackground(字符串…参数){ //TODO自动生成的方法存根 整数计数; String destination=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_下载)+“/”; 字符串[]文件名=参数; 字符串a=fileName.toString(); 字符串b=a.子字符串(20,25); 目的地+=b; 试一试{ URL=新URL(参数[0]); URLConnection conexion=url.openConnection(); conexion.connect(); int lengthofFile=conexion.getContentLength(); Log.d(“ANDRO_ASYNC”,“文件长度:“+lengthofFile”); InputStream输入=新的BufferedInputStream(url.openStream()); OutputStream输出=新文件OutputStream(目标); 字节数据[]=新字节[1024]; 长总计=0; 而((计数=输入。读取(数据))!=-1){ 总数+=计数; 出版进度(“+(int)((总计*100)/长办公室)); 输出.写入(数据,0,计数); } output.flush(); output.close(); input.close(); }捕获(例外e){ //TODO:处理异常 } 返回null; }
我的param变量将包含url链接,该链接为“”,我希望剪切该url..我希望的字符串为“ep123BCA.pdf” 但是当我运行这段代码时,substring方法总是会给我一个不同的子字符串来解决这个问题如何在java中使用带字符串数组的子字符串方法 class DownloadFileAsync扩展了AsyncTask{ @凌驾 受保护的字符串doInBackground(字符串…参数){ //TODO自动生成的方法存根 整数计数; String destination=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_下载)+“/”; 字符串[]文件名=参数; 字符串a=fileName.toString(); 字符串b=a.子字符串(20,25); 目的地+=b; 试一试{ URL=新URL(参数[0]); URLConnection conexion=url.openConnection(); conexion.connect(); int lengthofFile=conexion.getContentLength(); Log.d(“ANDRO_ASYNC”,“文件长度:“+lengthofFile”); InputStream输入=新的BufferedInputStream(url.openStream()); OutputStream输出=新文件OutputStream(目标); 字节数据[]=新字节[1024]; 长总计=0; 而((计数=输入。读取(数据))!=-1){ 总数+=计数; 出版进度(“+(int)((总计*100)/长办公室)); 输出.写入(数据,0,计数); } output.flush(); output.close(); input.close(); }捕获(例外e){ //TODO:处理异常 } 返回null; },java,android,Java,Android,我的param变量将包含url链接,该链接为“”,我希望剪切该url..我希望的字符串为“ep123BCA.pdf” 但是当我运行这段代码时,substring方法总是会给我一个不同的子字符串来解决这个问题 请提前提供帮助和帮助。使用lastIndexOf从URL获取文件名: class DownloadFileAsync extends AsyncTask<String, String, String> { @Override protected
请提前提供帮助和帮助。使用
lastIndexOf
从URL获取文件名:
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;
}
使用
lastIndexOf
从URL获取文件名:
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;
}
你可以用这个
String b = a.substring(a.lastIndexOf('/') + 1);
你可以用这个
String b = a.substring(a.lastIndexOf('/') + 1);
另一个解决方案是(如果url的第一部分从未更改)
这将按以下方式拆分字符串:
a.substring(22)
另一个解决方案是(如果url的第一部分从未更改)
这将按以下方式拆分字符串:
a.substring(22)
这一行将替换为您的代码
这一行将替换为您的代码。使用拆分方法
String[]parts=url.split(“/”);
它将url拆分为3或4个部分,最后一个部分是文件名,这是一种不适用于我的方法,它为我提供了与下载文件相同的垃圾名称。:(使用拆分方法String[]parts=url.split(“/”);
它会将url分成3或4部分,最后一部分是文件名,这是一种不适用于我的方法,它会给我一个与下载文件相同的垃圾名称。:(这是一种解决方案,但不够灵活。这肯定是正确的:)提供文件的垃圾名称。这是一个解决方案,但不够灵活。这肯定是正确的:)提供文件的垃圾名称。
filename = a.substring(a.lastIndexOf("/")+1);