[英]How do I Execute Java from Java?
我有這個DownloadFile.java
並按需下載文件:
import java.io.*;
import java.net.URL;
public class DownloadFile {
public static void main(String[] args) throws IOException {
String fileName = "setup.exe";
// The file that will be saved on your computer
URL link = new URL("http://onlinebackup.elgiganten.se/software/elgiganten/setup.exe");
// The file that you want to download
// Code to download
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1 != (n = in.read(buf))) {
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
// End download code
System.out.println("Finished");
}
}
我想從Gui.java
的鼠標事件執行此Gui.java
。
private void jLabel17MouseClicked(java.awt.event.MouseEvent evt){
}
我該怎么做呢?
您當前的方法是靜態方法,可以,但是它提取的所有數據都緊緊地保存在main方法中,從而防止了其他類使用它,但是幸運的是,可以對此進行更正。
我的建議:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.