# JavaFX-Demo **Repository Path**: Biubiuyuyu/JavaFX-Demo ## Basic Information - **Project Name**: JavaFX-Demo - **Description**: 使用JavaFX-Plus搭建的Demo, - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 14 - **Forks**: 7 - **Created**: 2020-01-09 - **Last Updated**: 2023-12-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 基于JavaFX-Plus的Demo ### 需求说明 下载的论文经常需要保存为特定的名字,但是如果下载后再修改成指定格式,总是有点麻烦,这就需要有一个可视化界面能够简化这个操作。 重点是展示下载过程的进度条的界面和下载实体通过JavaFX-plus绑定。 ### 效果展示 ![demo](doc/Demo.gif) ### 具体设计 #### 控制器 ```java @FXBind( {"url=${url.text}","savePath=${savePath.text}"} ) @FXData Downloader downloader =new Downloader(); @FXBind( { "year=${@to_int(year.text)}","firstAuthor=${author1.text}","secondAuthor=${author2.text}", "venue=${venue.text}","is_single=${is_single.selected}","name=${title.text}" } ) @FXData Paper paper = new Paper(); @FXBind( "progress=${downloader.download_rate}" ) @FXML private ProgressBar progressBar; ``` #### 实体类 ```java package model; import cn.edu.scau.biubiusuisui.annotation.FXEntity; import cn.edu.scau.biubiusuisui.annotation.FXField; import com.sun.tools.classfile.ConstantPool; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; @FXEntity public class Downloader { private int total; private int now; @FXField private String url; @FXField private String savePath; @FXField private Double download_rate = new Double(0.0); public void download(Paper paper) { Thread thread= new Thread(() -> { saveUrlAs(url, savePath, paper.toString(), "GET"); }); thread.setDaemon(true); thread.start(); } public File saveUrlAs(String url, String filePath, String filename, String method) { //System.out.println("fileName---->"+filePath); //创建不同的文件夹目录 File file = new File(filePath); //判断文件夹是否存在 if (!file.exists()) { //如果文件夹不存在,则创建新的的文件夹 file.mkdirs(); } FileOutputStream fileOut = null; HttpURLConnection conn = null; InputStream inputStream = null; try { // 建立链接 URL httpUrl = new URL(url); conn = (HttpURLConnection) httpUrl.openConnection(); //以Post方式提交表单,默认get方式 conn.setRequestMethod(method); conn.setDoInput(true); conn.setDoOutput(true); // post方式不能使用缓存 conn.setUseCaches(false); //连接指定的资源 conn.connect(); double total = Double.valueOf(conn.getHeaderField("Content-Length")); //System.out.println(length); //获取网络输入流 inputStream = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(inputStream); //判断文件的保存路径后面是否以/结尾 if (!filePath.endsWith("/")) { filePath += "/"; } //写入到文件(注意文件保存路径的后面一定要加上文件的名称) fileOut = new FileOutputStream(filePath + filename); BufferedOutputStream bos = new BufferedOutputStream(fileOut); byte[] buf = new byte[4096]; int length = bis.read(buf); int read_length = length; //保存文件 while (length != -1) { setDownload_rate((double) read_length / total); bos.write(buf, 0, length); length = bis.read(buf); read_length += length; } bos.close(); bis.close(); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); System.out.println("抛出异常!!"); } return file; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getNow() { return now; } public void setNow(int now) { this.now = now; } public double getDownload_rate() { return download_rate; } public void setDownload_rate(double download_rate) { this.download_rate = download_rate; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public void setDownload_rate(Double download_rate) { this.download_rate = download_rate; } } ``` ```java package model; import cn.edu.scau.biubiusuisui.annotation.FXEntity; import cn.edu.scau.biubiusuisui.annotation.FXField; @FXEntity public class Paper { @FXField String name; @FXField String venue; @FXField String firstAuthor; @FXField String secondAuthor; @FXField Boolean is_single = new Boolean(false); @FXField int year; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVenue() { return venue; } public void setVenue(String venue) { this.venue = venue; } public String getFirstAuthor() { return firstAuthor; } public void setFirstAuthor(String firstAuthor) { this.firstAuthor = firstAuthor; } public String getSecondAuthor() { return secondAuthor; } public void setSecondAuthor(String secondAuthor) { this.secondAuthor = secondAuthor; } public Boolean getIs_single() { return is_single; } public void setIs_single(Boolean is_single) { this.is_single = is_single; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } @Override public String toString() { if(is_single) { return String.format("(%s. %s %d)%s.pdf",firstAuthor,venue,year,name); }else{ if(null == secondAuthor || "".equals(secondAuthor)){ return String.format("(%s et al. %s %d)%s.pdf",firstAuthor,venue,year,name); }else{ return String.format("(%s and %s. %s %d)%s.pdf",firstAuthor,secondAuthor,venue,year,name); } } } } ```