Java 从internet下载文件并对其中的一些文件进行子串
我正在创建一个从internet下载文件的程序。 我给链接添加子字符串,因为我想从链接中获取文件名。 这就是我目前所拥有的Java 从internet下载文件并对其中的一些文件进行子串,java,download,Java,Download,我正在创建一个从internet下载文件的程序。 我给链接添加子字符串,因为我想从链接中获取文件名。 这就是我目前所拥有的 package main; import java.io.FileOutputStream; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; public class Main { public stati
package main;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class Main {
public static void main(String[] args) {
String url = "https://www.dropbox.com/s/8awaehjdh81fqam/CacheName.zip?dl=1";
int lastSlashIndex = url.lastIndexOf('/');
String filename= url.substring(lastSlashIndex + 1, 5);
try{
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(filename);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}catch(Exception e){ e.printStackTrace(); }
}
}
它没有下载该文件,我收到以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -37
at java.lang.String.substring(Unknown Source)
at main.Main.main(Main.java:14)
@Hypino已经回答了你的问题,但是我将使用正则表达式来实现这个目的。在我看来,对于这种情况,这是最简单、最易维护的解决方案:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DropboxAddressFileName {
public static void main(String args[]) {
String address = "https://www.dropbox.com/s/8awaehjdh81fqam/CacheName.zip?dl=1";
Pattern p = Pattern.compile("^.*/(.*)\\?.*$");
Matcher m = p.matcher(address);
String match = m.group(1);
System.out.println(match);
}
}
您将在输出中获得
CacheName.zip
。@Hypino已经回答了您的问题,但我将仅使用regex实现此目的。在我看来,对于这种情况,这是最简单、最易维护的解决方案:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DropboxAddressFileName {
public static void main(String args[]) {
String address = "https://www.dropbox.com/s/8awaehjdh81fqam/CacheName.zip?dl=1";
Pattern p = Pattern.compile("^.*/(.*)\\?.*$");
Matcher m = p.matcher(address);
String match = m.group(1);
System.out.println(match);
}
}
您将在输出中获得
CacheName.zip
。根据Java文档,公共字符串子字符串(int-beginIndex,int-endIndex)
是如何调用子字符串方法的。按照您的调用方式,它从lastslasslashindex+1
开始,在索引5结束(向后,不起作用)。这就是为什么例外情况是说它超出了指数-37的范围
您需要更像
url.substring(lastSlashIndex+1,lastslasslashindex+1+5)的东西代码>根据Java文档,公共字符串子字符串(int beginIndex,int endIndex)
是如何调用子字符串方法的。按照您的调用方式,它从lastslasslashindex+1
开始,在索引5结束(向后,不起作用)。这就是为什么例外情况是说它超出了指数-37的范围
您需要更像url.substring(lastSlashIndex+1,lastslasslashindex+1+5)的东西代码>-37肯定超出了字符串的范围…你读到错误了吗?我读到了错误是的,但我不知道它为什么会显示-37?-37肯定超出了字符串的范围…你读到错误了吗?我读到了错误是的,但我不知道它为什么会显示-37?谢谢,我现在没有收到任何错误。但是下载后这个文件放在哪里呢?FileOutputStream写入的文件应该放在您当前的工作目录中(或者文件对象的路径指向的任何地方)。谢谢,如果我想更改目录,我必须这样做吗?FileOutputStream fos=新的FileOutputStream(目录+文件名);谢谢,我现在没有任何错误。但是下载后这个文件放在哪里呢?FileOutputStream写入的文件应该放在您当前的工作目录中(或者文件对象的路径指向的任何地方)。谢谢,如果我想更改目录,我必须这样做吗?FileOutputStream fos=新的FileOutputStream(目录+文件名);我将其更改为:String filename=url.substring(lastslaslassindex+1,url.length()-5);我不明白PI将其更改为:String filename=url.substring(lastSlashIndex+1,url.length()-5);我不明白P