java小程序HTTP下载文件无法工作
java applet HTTP download file can not work
我编写了简单的小程序来从 HTTP URL 下载文件。
在 Eclipse 或 Netbeans 中,它运行良好,可以将文件下载到我硬盘上的 d://abc//123.iso。
这是我的代码:
public class download {
public static void saveUrl(final String filename, final String urlString)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(filename,true);
final byte data[] = new byte[1024];
int count;
fout.write(data, 0, count);
} finally {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
}
}
}
public class HelloWorldApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Download file", 25, 50);
String url ="http://downloads.asterisk.org/pub/telephony/asterisk-now/AsteriskNOW-612-current-32.iso";
String file_out = "d:\abc\123.iso";
download.saveUrl(file_out, url);
}
}
============================
但是当使用 html 导出到 jar 文件和 运行 时,浏览器可以在我的硬盘上创建新文件 123.iso 但这个文件的大小总是 2 Kbps。我认为它不下载任何东西。
请帮我
非常感谢
P/s : 我尝试使用 jarsigner 对 jar 文件进行签名,但它并没有解决问题
您正在写输入中的第一次读取。您需要写入文件,直到输入为空。
在编写代码时试试这个
while ((count = in.read(data)) != -1) {
fout.write(data, 0, count);
...
}
虽然我怀疑上面的代码是否能像发布的那样执行任何操作,但即使编译,这里是我用于自动更新下载大型(>100 MB)文件的解决方案:
HttpGet httpGet;
RequestConfig requestConfig;
getProxySettings();
//Check to see if there is a proxy availabble.
if (!LicensePreloader.proxyAddr.equals("")) {
requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setProxy(new HttpHost(LicensePreloader.proxyAddr, LicensePreloader.proxyPort))
.build();
} else {
//No proxy was available, just use regular internet.
requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
}
httpGet = new HttpGet(this.remoteUrl);
HttpResponse response;
InputStream remoteContentStream = null;
OutputStream localFileStream = null;
try {
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
//This builds the content of our file we're downloading.
remoteContentStream = response.getEntity().getContent();
long fileSize = response.getEntity().getContentLength();
File dir = localFile.getParentFile();
dir.mkdirs();
localFileStream = new FileOutputStream(localFile);
//Set the buffer, in our use case, it's always the deafult 8192 bytes.
byte[] buffer = new byte[bufferSize];
int sizeOfChunk;
int amountComplete = 0;
//Simply loop through and download the file in 'chunks'
while ((sizeOfChunk = remoteContentStream.read(buffer)) != -1) {
localFileStream.write(buffer, 0, sizeOfChunk);
amountComplete += sizeOfChunk;
updateProgress(amountComplete, fileSize);
}
return localFile;
} finally {
//Make sure to clean everything up.
try {
if (remoteContentStream != null) {
remoteContentStream.close();
}
if (localFileStream != null) {
localFileStream.close();
}
} catch (IOException ex) {
//If we're here, it's likely because the internet conneciton
//couldn't be established, or it was cut short in the middle.
ex.printStackTrace(System.out);
failed();
}
}
}
这对于您的应用程序来说显然是多余的,您可能会忘记所有代理业务,但为了完整起见,我将其保留在那里。有几个我没有包括的辅助方法,但同样,它们几乎都专门用于代理处理。
祝你好运!
我编写了简单的小程序来从 HTTP URL 下载文件。 在 Eclipse 或 Netbeans 中,它运行良好,可以将文件下载到我硬盘上的 d://abc//123.iso。 这是我的代码:
public class download {
public static void saveUrl(final String filename, final String urlString)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(filename,true);
final byte data[] = new byte[1024];
int count;
fout.write(data, 0, count);
} finally {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
}
}
}
public class HelloWorldApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Download file", 25, 50);
String url ="http://downloads.asterisk.org/pub/telephony/asterisk-now/AsteriskNOW-612-current-32.iso";
String file_out = "d:\abc\123.iso";
download.saveUrl(file_out, url);
}
}
============================
但是当使用 html 导出到 jar 文件和 运行 时,浏览器可以在我的硬盘上创建新文件 123.iso 但这个文件的大小总是 2 Kbps。我认为它不下载任何东西。 请帮我 非常感谢 P/s : 我尝试使用 jarsigner 对 jar 文件进行签名,但它并没有解决问题
您正在写输入中的第一次读取。您需要写入文件,直到输入为空。
在编写代码时试试这个
while ((count = in.read(data)) != -1) {
fout.write(data, 0, count);
...
}
虽然我怀疑上面的代码是否能像发布的那样执行任何操作,但即使编译,这里是我用于自动更新下载大型(>100 MB)文件的解决方案:
HttpGet httpGet;
RequestConfig requestConfig;
getProxySettings();
//Check to see if there is a proxy availabble.
if (!LicensePreloader.proxyAddr.equals("")) {
requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setProxy(new HttpHost(LicensePreloader.proxyAddr, LicensePreloader.proxyPort))
.build();
} else {
//No proxy was available, just use regular internet.
requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
}
httpGet = new HttpGet(this.remoteUrl);
HttpResponse response;
InputStream remoteContentStream = null;
OutputStream localFileStream = null;
try {
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
//This builds the content of our file we're downloading.
remoteContentStream = response.getEntity().getContent();
long fileSize = response.getEntity().getContentLength();
File dir = localFile.getParentFile();
dir.mkdirs();
localFileStream = new FileOutputStream(localFile);
//Set the buffer, in our use case, it's always the deafult 8192 bytes.
byte[] buffer = new byte[bufferSize];
int sizeOfChunk;
int amountComplete = 0;
//Simply loop through and download the file in 'chunks'
while ((sizeOfChunk = remoteContentStream.read(buffer)) != -1) {
localFileStream.write(buffer, 0, sizeOfChunk);
amountComplete += sizeOfChunk;
updateProgress(amountComplete, fileSize);
}
return localFile;
} finally {
//Make sure to clean everything up.
try {
if (remoteContentStream != null) {
remoteContentStream.close();
}
if (localFileStream != null) {
localFileStream.close();
}
} catch (IOException ex) {
//If we're here, it's likely because the internet conneciton
//couldn't be established, or it was cut short in the middle.
ex.printStackTrace(System.out);
failed();
}
}
}
这对于您的应用程序来说显然是多余的,您可能会忘记所有代理业务,但为了完整起见,我将其保留在那里。有几个我没有包括的辅助方法,但同样,它们几乎都专门用于代理处理。
祝你好运!