关键代码如下
public void showHtml() {
WebEngine webEngine = htmlContent.getEngine();
webEngine.loadContent("<h1>题目</h1><img style='width:100px;height:50px;' src='https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png' />");
}
public void editHtml() {
Stage stage = new Stage();
final HTMLEditor htmlEditor = new HTMLEditor();
stage.setScene(new Scene(htmlEditor));
stage.setTitle("文档编辑器");
stage.show();
Node node = htmlEditor.lookup(".top-toolbar");
if (node instanceof ToolBar) {
ToolBar bar = (ToolBar) node;
Button btn = new Button("上传图片");
btn.setPrefSize(100, 30);
bar.getItems().add(btn);
String img = "<img alt=\"Embedded Image\" src=\"http://doc.xiaoyou66.com/Public/Uploads/2020-07-05/5f0138fd785f2.png\" />";
btn.setOnAction((ActionEvent event) -> {
htmlEditor.setHtmlText(htmlEditor.getHtmlText() + img);
});
Button btn2 = new Button("获取内容");
bar.getItems().add(btn2);
btn2.setPrefSize(100, 30);
btn2.setOnAction(event -> {
System.out.println(htmlEditor.getHtmlText());
});
}
WebEngine webEngine = htmlContent.getEngine();
htmlEditor.setHtmlText((String) webEngine.executeScript("document.documentElement.outerHTML"));
AtomicReference<String> content = new AtomicReference<>("");
htmlEditor.setOnDragEntered(event -> {
System.out.println("拖动事件,图片地址:"+ event.getDragboard().getFiles().get(0).getAbsolutePath());
content.set(htmlEditor.getHtmlText());
});
htmlEditor.setOnDragExited(event -> {
String url = event.getDragboard().getFiles().get(0).getAbsolutePath();
htmlEditor.setHtmlText(content.get() + "<img src='file:///" + url + "'/>");
});
htmlEditor.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.isControlDown() && e.getCode() == KeyCode.V) {
System.out.println("粘贴事件。。。。");
htmlEditor.setHtmlText(htmlEditor.getHtmlText() + "<img src='data:image/jpeg;base64, " + modifyClipboard() + "'/>");
}
});
stage.setOnCloseRequest(event -> {
webEngine.loadContent(htmlEditor.getHtmlText());
});
}
public static Image getImageFromClipboard() throws Exception {
java.awt.datatransfer.Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable cc = sysc.getContents(null);
if (cc == null) {
return null;
} else if (cc.isDataFlavorSupported(DataFlavor.imageFlavor)) {
return (Image) cc.getTransferData(DataFlavor.imageFlavor);
}
return null;
}
private String modifyClipboard() {
Clipboard clipboard = Clipboard.getSystemClipboard();
System.out.println("从剪贴板获取文件路径:" + clipboard.getFiles());
System.out.println("从剪贴板获取图片:" + clipboard.getImage());
System.out.println("从剪贴板获取文字:" + clipboard.getString());
System.out.println("从剪贴获取文件类型:" + clipboard.getContentTypes());
if(clipboard.getImage()!=null){
saveFile();
}
return "";
}
private void saveFile(){
try {
Image image = getImageFromClipboard();
File file = new File("D:\\fileUpload\\1.png");
BufferedImage bufferedImage = new BufferedImage((int) image.getWidth(null), (int) image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, null, null);
ImageIO.write((RenderedImage) bufferedImage, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
}
@FXML
private void uploadTest(){
System.out.println("上传图片测试");
uploadImg("C:\\Users\\Administrator\\Pictures\\image.png");
}
public void uploadImg(String fileName) {
try {
final String newLine = "\r\n";
final String boundaryPrefix = "--";
String BOUNDARY = "========7d4a6d158c9";
URL url = new URL(API+"/upload");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
File file = new File(fileName);
StringBuilder sb = new StringBuilder();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + fileName
+ "\"" + newLine);
sb.append("Content-Type:image/jpeg");
sb.append(newLine);
sb.append(newLine);
out.write(sb.toString().getBytes());
DataInputStream in = new DataInputStream(new FileInputStream(file));
byte[] bufferOut = new byte[1024];
int bytes = 0;
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
out.write(newLine.getBytes());
in.close();
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine)
.getBytes();
out.write(end_data);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
final String str ="\"data\":\"(.*?)\"";
Pattern p= Pattern.compile(str);
Matcher m=p.matcher(line);
if(m.find()) {
String data = m.group();
System.out.println(API+data.substring(8,data.length()-1));
}
}
} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
}