我创造了这个功能..。
void DownloadFromDatabase() throws IOException {
URL website = new URL("http://theurlofmywebsite.org/databases/record_file.txt");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("record_file.txt");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}..。当我点击一个按钮的时候我就叫它,你可以在这里看到。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
DownloadFromDatabase();
} catch (IOException ex) {
Logger.getLogger(xGrep.class.getName()).log(Level.SEVERE, null, ex);
}
}当我单击该按钮时,会调用DownloadFromDatabase();,但我看不到桌面上的文件record_file.txt。你知道为什么吗?
发布于 2013-07-28 13:23:07
这段代码不是最好的,但我已经在我的电脑上做了一个测试,它可以工作。它在2秒内下载500行文本文件。
void DownloadFromDatabase() throws MalformedURLException, IOException {
URLConnection conn = new URL("your_url_here").openConnection();
InputStream is = conn.getInputStream();
OutputStream outstream = new FileOutputStream(new File("filename.txt"));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
}我把它命名为DownloadFromDatabase(),所以您只需复制/粘贴这段代码,而不是您的代码。另外,除了例外情况,也要注意。
https://stackoverflow.com/questions/17908410
复制相似问题