更新http 工具 2023年3月8日17:03:32
parent
cbd00e53c6
commit
7a6c0f7d20
16
README.md
16
README.md
|
|
@ -108,6 +108,22 @@
|
|||
|
||||

|
||||
|
||||
### 7.http 工具
|
||||
|
||||
> 使用hutool工具
|
||||
|
||||
### 7.1 http 请求工具
|
||||
|
||||

|
||||
|
||||
#### 7.2 上传工具
|
||||
|
||||

|
||||
|
||||
#### 7.3 下载工具
|
||||
|
||||

|
||||
|
||||
## 2. 开源项目总览
|
||||
|
||||
| 项目名称 |地址|
|
||||
|
|
|
|||
|
|
@ -1,7 +1,34 @@
|
|||
package com.zhangmeng.tools.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.StreamProgress;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.zhangmeng.tools.utils.AlertUtils;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.concurrent.Service;
|
||||
import javafx.concurrent.Task;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.stage.DirectoryChooser;
|
||||
import javafx.stage.Stage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
|
|
@ -9,4 +36,148 @@ import lombok.extern.slf4j.Slf4j;
|
|||
*/
|
||||
@Slf4j
|
||||
public class HttpDownLoadController {
|
||||
|
||||
@FXML
|
||||
public ProgressBar progress;
|
||||
|
||||
@FXML
|
||||
public TextField url;
|
||||
|
||||
@FXML
|
||||
public Button download;
|
||||
|
||||
@FXML
|
||||
public TextArea download_info;
|
||||
|
||||
@FXML
|
||||
private TextField download_path;
|
||||
|
||||
@FXML
|
||||
private Button choose_file;
|
||||
|
||||
private final SimpleObjectProperty<File> file_target = new SimpleObjectProperty<>();
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
|
||||
choose_file.setOnAction(event -> {
|
||||
Stage stage = new Stage();
|
||||
DirectoryChooser dc = new DirectoryChooser();
|
||||
dc.setTitle("文件夹选择器");
|
||||
File file = dc.showDialog(stage);
|
||||
if (file != null){
|
||||
download_path.setText(file.getPath());
|
||||
file_target.setValue(file);
|
||||
}
|
||||
});
|
||||
|
||||
download.setOnAction(event -> {
|
||||
|
||||
if (url.getText().length() == 0 ){
|
||||
AlertUtils.alert_warning("请输入下载文件的地址再试!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (download_path.getText().length() == 0){
|
||||
AlertUtils.alert_warning("请输入文件保存地址再试!");
|
||||
return;
|
||||
}
|
||||
|
||||
MyService myService = new MyService();
|
||||
myService.progressProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if (newValue.doubleValue() >= 1){
|
||||
myService.cancel();
|
||||
log.info("任务取消!.......");
|
||||
}else {
|
||||
progress.setProgress(newValue.doubleValue());
|
||||
}
|
||||
});
|
||||
|
||||
myService.messageProperty().addListener(new ChangeListener<String>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
|
||||
if(newValue != null){
|
||||
download_info.appendText(newValue);
|
||||
download_info.appendText(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
});
|
||||
myService.start();
|
||||
});
|
||||
}
|
||||
|
||||
class MyService extends Service<Number>{
|
||||
|
||||
@Override
|
||||
protected Task<Number> createTask() {
|
||||
return new Task<Number>() {
|
||||
@Override
|
||||
protected Number call(){
|
||||
int size = 0;
|
||||
try {
|
||||
URL url1 = new URL(url.getText());
|
||||
URLConnection conn = url1.openConnection();
|
||||
size = conn.getContentLength();
|
||||
if (size < 0) {
|
||||
download_info.appendText("无法获取文件大小。");
|
||||
download_info.appendText(System.lineSeparator());
|
||||
}else{
|
||||
download_info.appendText("文件大小为:" + size + " bytes");
|
||||
download_info.appendText(System.lineSeparator());
|
||||
|
||||
String fileName;
|
||||
final String path = url.getText();
|
||||
// 从路径中获取文件名
|
||||
fileName = StrUtil.subSuf(path, path.lastIndexOf('/') + 1);
|
||||
if (StrUtil.isBlank(fileName)) {
|
||||
// 编码后的路径做为文件名
|
||||
fileName = URLUtil.encodeQuery(path, CharsetUtil.CHARSET_UTF_8);
|
||||
}
|
||||
String pathText = download_path.getText();
|
||||
|
||||
InputStream fis = conn.getInputStream();
|
||||
int max = size;
|
||||
|
||||
FileOutputStream fos = new FileOutputStream(new File(pathText + "/" + fileName));
|
||||
|
||||
byte[] bytes = new byte[100000];
|
||||
|
||||
int i = 0;
|
||||
|
||||
double sum = 0 ;
|
||||
|
||||
double progress = 0;
|
||||
this.updateMessage("开始下载:-------->" + pathText + "/" + fileName);
|
||||
while ((i = fis.read(bytes, 0, bytes.length)) != -1) {
|
||||
|
||||
if (this.isCancelled()){
|
||||
break;
|
||||
}
|
||||
|
||||
fos.write(bytes,0,i);
|
||||
|
||||
sum = sum + i;
|
||||
|
||||
this.updateProgress(sum,max);
|
||||
|
||||
progress = sum /max;
|
||||
|
||||
this.updateMessage("已下载:" + FileUtil.readableFileSize((long) sum));
|
||||
}
|
||||
|
||||
fis.close();
|
||||
fos.close();
|
||||
|
||||
this.updateMessage("下载结束:-------->" + FileUtil.readableFileSize(size));
|
||||
return progress;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
download_info.appendText(e.getMessage());
|
||||
download_info.appendText(System.lineSeparator());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -243,14 +243,14 @@ public class HttpToolsController {
|
|||
}
|
||||
|
||||
if (newValue.getIndex() == 1) {
|
||||
if (http_request != null){
|
||||
if (http_upload != null){
|
||||
flag = true;
|
||||
}
|
||||
http_upload(flag);
|
||||
}
|
||||
|
||||
if (newValue.getIndex() == 2) {
|
||||
if (http_request != null){
|
||||
if (http_download != null){
|
||||
flag = true;
|
||||
}
|
||||
http_download(flag);
|
||||
|
|
@ -349,9 +349,9 @@ public class HttpToolsController {
|
|||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
http_request = root;
|
||||
http_upload = root;
|
||||
}else {
|
||||
root = http_request;
|
||||
root = http_upload;
|
||||
}
|
||||
common_method();
|
||||
}
|
||||
|
|
@ -366,9 +366,9 @@ public class HttpToolsController {
|
|||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
http_request = root;
|
||||
http_download = root;
|
||||
}else {
|
||||
root = http_request;
|
||||
root = http_download;
|
||||
}
|
||||
common_method();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,23 @@
|
|||
package com.zhangmeng.tools.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.zhangmeng.tools.utils.AlertUtils;
|
||||
import com.zhangmeng.tools.utils.ImagePath;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
|
|
@ -20,21 +29,67 @@ public class HttpUploadController {
|
|||
@FXML
|
||||
public Button upload_button;
|
||||
|
||||
@FXML
|
||||
public Button choose_file_button;
|
||||
|
||||
@FXML
|
||||
public TextArea upload_result;
|
||||
|
||||
@FXML
|
||||
public TextField url;
|
||||
|
||||
@FXML
|
||||
private TextField file_path;
|
||||
|
||||
private final SimpleObjectProperty<File> file_vale = new SimpleObjectProperty<>();
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
upload_button.setText(null);
|
||||
|
||||
upload_result.setWrapText(true);
|
||||
|
||||
choose_file_button.setText(null);
|
||||
ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE)));
|
||||
iv.setPreserveRatio(true);
|
||||
iv.setFitWidth(18);
|
||||
upload_button.setGraphic(iv);
|
||||
choose_file_button.setGraphic(iv);
|
||||
|
||||
choose_file_button.setOnAction(event -> {
|
||||
Stage stage = new Stage();
|
||||
FileChooser fc = new FileChooser();
|
||||
fc.setTitle("单选文件");
|
||||
fc.getExtensionFilters().addAll(
|
||||
new FileChooser.ExtensionFilter("所有类型", "*.*")
|
||||
);
|
||||
|
||||
File file = fc.showOpenDialog(stage);
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
file_path.setText(file.getAbsolutePath());
|
||||
file_vale.setValue(file);
|
||||
});
|
||||
|
||||
upload_button.setOnAction(event -> {
|
||||
|
||||
if (url.getText().length() == 0){
|
||||
AlertUtils.alert_warning("请输入上传接口再试!");
|
||||
return;
|
||||
}
|
||||
|
||||
HashMap<String, Object> paramMap = new HashMap<>();
|
||||
//文件上传只需将参数中的键指定(默认file),值设为文件对象即可,对于使用者来说,文件上传与普通表单提交并无区别
|
||||
|
||||
if (file_vale.getValue() == null){
|
||||
AlertUtils.alert_warning("请选择文件后再试!");
|
||||
return;
|
||||
}
|
||||
|
||||
paramMap.put("file", file_vale.getValue());
|
||||
|
||||
String result= HttpUtil.post(url.getText(), paramMap);
|
||||
upload_result.setText(result);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import java.util.*?>
|
||||
<?import javafx.scene.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
|
||||
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="fxml.HttpDownload" prefHeight="649" prefWidth="1200">
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.ProgressBar?>
|
||||
<?import javafx.scene.control.TextArea?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
<AnchorPane prefHeight="649" prefWidth="1200" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.HttpDownLoadController">
|
||||
<children>
|
||||
<ProgressBar fx:id="progress" layoutX="165.0" layoutY="237.0" prefHeight="17.0" prefWidth="954.0" progress="0.0" AnchorPane.leftAnchor="165.0" AnchorPane.rightAnchor="81.0" />
|
||||
<Label layoutX="86.0" layoutY="237.0" text="下载进度条:" />
|
||||
<Label layoutX="86.0" layoutY="161.0" text="下载文件地址:" />
|
||||
<TextField fx:id="url" layoutX="165.0" layoutY="157.0" prefHeight="25.0" prefWidth="954.0" AnchorPane.leftAnchor="165.0" AnchorPane.rightAnchor="81.0" />
|
||||
<Button fx:id="download" layoutX="580.0" layoutY="276.0" mnemonicParsing="false" text="下载" />
|
||||
<Label layoutX="86.0" layoutY="88.0" text="下载文件夹:" />
|
||||
<TextField fx:id="download_path" layoutX="165.0" layoutY="84.0" prefHeight="25.0" prefWidth="461.0" />
|
||||
<Button fx:id="choose_file" layoutX="642.0" layoutY="84.0" mnemonicParsing="false" text="选择文件" AnchorPane.leftAnchor="642.0" />
|
||||
<Label layoutX="108.0" layoutY="369.0" text="结果:" />
|
||||
<TextArea fx:id="download_info" layoutX="165.0" layoutY="378.0" prefHeight="200.0" prefWidth="954.0" AnchorPane.bottomAnchor="71.0" AnchorPane.leftAnchor="165.0" AnchorPane.rightAnchor="81.0" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
|
|
|
|||
|
|
@ -6,14 +6,16 @@
|
|||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
|
||||
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.HttpUploadController">
|
||||
<children>
|
||||
<Label layoutX="190.0" layoutY="157.0" text="选择文件:" />
|
||||
<Button fx:id="upload_button" layoutX="306.0" layoutY="153.0" mnemonicParsing="false" text="Button" />
|
||||
<Button fx:id="choose_file_button" layoutX="306.0" layoutY="153.0" mnemonicParsing="false" text="Button" />
|
||||
<TextArea fx:id="upload_result" layoutX="306.0" layoutY="274.0" prefHeight="200.0" prefWidth="776.0" AnchorPane.bottomAnchor="175.0" AnchorPane.leftAnchor="306.0" AnchorPane.rightAnchor="118.0" AnchorPane.topAnchor="274.0" />
|
||||
<Label layoutX="190.0" layoutY="266.0" text="上传结果:" />
|
||||
<Label layoutX="190.0" layoutY="85.0" text="上传接口:" />
|
||||
<TextField fx:id="url" layoutX="306.0" layoutY="81.0" prefHeight="25.0" prefWidth="776.0" AnchorPane.leftAnchor="306.0" AnchorPane.rightAnchor="118.0" />
|
||||
<TextField fx:id="file_path" layoutX="464.0" layoutY="153.0" prefHeight="25.0" prefWidth="614.0" />
|
||||
<Label layoutX="397.0" layoutY="157.0" text="文件路径:" />
|
||||
<Button fx:id="upload_button" layoutX="306.0" layoutY="518.0" mnemonicParsing="false" text="上传" AnchorPane.bottomAnchor="106.0" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
|
|
|
|||
Binary file not shown.
|
After Width: | Height: | Size: 69 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 125 KiB |
Loading…
Reference in New Issue