我如何从java应用程序下载一些东西,然后把它放到桌面上?
大家好,我想做一个应用程序,它从网站上下载一些东西,并把它放在桌面上 这段代码下载了它,但暂时,我该如何保存它呢 这是我的密码我如何从java应用程序下载一些东西,然后把它放到桌面上?,java,download,Java,Download,大家好,我想做一个应用程序,它从网站上下载一些东西,并把它放在桌面上 这段代码下载了它,但暂时,我该如何保存它呢 这是我的密码 private static void grabItem() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, UnsupportedLookAndFeelException { final
private static void grabItem() throws ClassNotFoundException,
InstantiationException, IllegalAccessException, IOException,
UnsupportedLookAndFeelException {
final URL url = new URL("sampleurl");
final InputStream is = url.openStream();
final byte[] b = new byte[2048];
int length;
final HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// Connect to server.
connection.connect();
// Make sure response code is in the 200 range.
if ((connection.getResponseCode() / 100) != 2) {
logger.info("Unable to find file");
return;
}
// set content length.
size = connection.getContentLength();
while ((length = is.read(b)) != -1) {
downloaded += length;
progressBar.setValue((int) getProgress()); // set progress bar
}
is.close();
setFrameTheme();
}
谢谢您从未将任何数据写入计算机。。。但是无论如何 这就是我下载和保存文件的方式。。。它需要是一个直接下载,但它很容易改变它的工作方式,你想要它
URL url = new URL("direct link goes here");
URLConnection connection = url.openConnection();
InputStream inputstream = connection.getInputStream();
为了让它拯救你,你会
BufferedOuputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(new File("location to save downloaded file")));
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = inputstream.read(buffer)))
{
bufferedoutputstream.write(buffer, 0, bytesRead);
}
bufferedoutputstream.flush();
bufferedoutputstream.close();
inputstream.close();
那应该下载并保存为什么你觉得有必要抛出ClassNotFoundException.download。将文件放入桌面目录。或者你的意思是想让它成为桌面背景?把它放在桌面目录中你试过什么?出了什么问题?请帮忙!!对你和我们都没有任何帮助。