Oppure

Loading
08/05/13 10:01
lillogoal
Salve a tutti,
sto creando un downloader del nuovo hosting MEGA.CO.NZ di Kim Dotcom.
Ho un problema:vorrei visualizzare in un label la velocità di download e la dimensione rimasta da scaricare del file.
Tutto questo mentre sto scrivendo il file su disco.
Ecco il codice della void download:
 public void download(String url, String path) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalBlockSizeException, BadPaddingException, JSONException, InterruptedException{
		
		print("Download started");
                JOptionPane.showMessageDialog(fr, "Download Iniziato", "Information", JOptionPane.INFORMATION_MESSAGE);

		String[] s = url.split("!");
		String file_id = s[1];
		byte[] file_key = MegaCrypt.base64_url_decode_byte(s[2]); 

		int[] intKey = MegaCrypt.aByte_to_aInt(file_key);
		JSONObject json = new JSONObject();
		try {
			json.put("a", "g");
			json.put("g", "1");
			json.put("p", file_id);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		JSONObject file_data;
                file_data = new JSONObject(api_request(json.toString()));
                
		int[] keyNOnce = new int[] { intKey[0] ^ intKey[4], intKey[1] ^ intKey[5], intKey[2] ^ intKey[6], intKey[3] ^ intKey[7], intKey[4], intKey[5] };
		byte[] key = MegaCrypt.aInt_to_aByte(keyNOnce[0], keyNOnce[1], keyNOnce[2], keyNOnce[3]);

		int[] iiv = new int[] { keyNOnce[4], keyNOnce[5], 0, 0 };
		byte[] iv = MegaCrypt.aInt_to_aByte(iiv);

		@SuppressWarnings("unused")
		int file_size = file_data.getInt("s");
                file_sizes = (((file_size) / 1024) / 1024);
                filesize.setText( (file_sizes) + "MB");
                filesize.setVisible(true);
                             
		String attribs = (file_data.getString("at"));
		attribs = new String(MegaCrypt.aes_cbc_decrypt(MegaCrypt.base64_url_decode_byte(attribs), key));

		file_name = attribs.substring(10,attribs.lastIndexOf("\""));
		print(file_name);
                filename.setText("Nome File:" + file_name);
                filename.setVisible(true);
		final IvParameterSpec ivSpec = new IvParameterSpec(iv);
		final SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES/CTR/nopadding");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
		InputStream is = null;
		String file_url = file_data.getString("g");
		FileOutputStream fos = new FileOutputStream(path+File.separator+file_name);
		final OutputStream cos = new CipherOutputStream(fos, cipher);
		final Cipher decipher = Cipher.getInstance("AES/CTR/NoPadding");
	        decipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
                
try {
			URLConnection urlConn = new URL(file_url).openConnection();
			print(file_url);
			is = urlConn.getInputStream();
			while ((read = is.read(buffer)) > 0)
                        { 
			    cos.write(buffer, 0, read);
                            speed = ((((file_size)/1024)/1024) - (((read)/1024)/1024));
                            aString = Double.toString(speed);
                            print(speed);
                            System.out.println(read);+

                            dwspd.setText("Velocità Download" + speed);     
			}                 
		} finally {
			try {
				cos.close();
				if (is != null) {
					is.close();
				}
			} finally {
				if (fos != null) {
					fos.close();
				}
			}
		}
                JOptionPane.showMessageDialog(fr, "Download Completato", "Information", JOptionPane.INFORMATION_MESSAGE);
		print("Download finished");

dwspd è una label... e vorrei che ad ogni ciclo del while venga settata. però non si vede neanche... Solo quando finisce di scaricare il file viene settata con l'ultimo numero!
Colgo l'occasione per chiedere ... se qualcuno vuole realizzare questo progetto insieme a me ;)
Saluti
Lorenzo
Ultima modifica effettuata da lillogoal 08/05/13 10:03
aaa
08/05/13 16:16
LittleHacker
Devi usare il multitasking!
Ecco un bel esempio: technology.amis.nl/2009/02/19/asynchronous-processing-in-java-applications-leveraging-those-multi-cores/, qui vai ad inserire il codice che scrive sulla label la percentuale! :k:
aaa
08/05/13 17:57
lillogoal
Grazie mille, ci avevo pensato che fosse un problema di thread! Solo che non ho mai usato quelle classi.
Non è che potresti provare a farmelo nel mio caso? perchè ho cercato anche su internet ma non riesco bene a capire! :( grazie ancora
aaa
08/05/13 20:56
LittleHacker
Postato originariamente da lillogoal:

Grazie mille, ci avevo pensato che fosse un problema di thread! Solo che non ho mai usato quelle classi.
Non è che potresti provare a farmelo nel mio caso? perchè ho cercato anche su internet ma non riesco bene a capire! :( grazie ancora


Non programmo più in java! Comunque puoi sempre aspettare la risposta dei più guru in JAVA! :k:
aaa
09/05/13 11:03
lillogoal
Ti ringrazio lo stesso :) aspetto fiducioso altre risposte ;)
aaa
15/05/13 16:33
lillogoal
Potete chiudere ho risolto! Ho utilizzato lo SwingWorker semplice e intuitivo ;)
aaa