-2
嗨,我很想做一個應用程序,它從網站上下載一些東西放到桌面上。我將如何去從java應用程序下載東西,然後把它放在桌面上?
此代碼下載它,但暫時,我將如何去保存它?
繼承人我的代碼
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();
}
感謝
爲什麼地球上,你覺得有必要拋出'ClassNotFoundException'。 – Makoto
下載。將該文件放入桌面目錄中。 (或者你的意思是你想讓它成爲桌面背景?) – keshlam
把它放在桌面目錄 – Boolena