add file parse

This commit is contained in:
N1KO 2024-11-22 13:37:15 +08:00
parent 3ae12ece91
commit 9738a23451
2 changed files with 201 additions and 0 deletions

View File

@ -23,4 +23,10 @@ public class StaticController {
// 这里返回的字符串是HTML文件名不包括扩展名
return "coin";
}
@RequestMapping("/file-parse")
public String viewFileParseHtml() {
// 这里返回的字符串是HTML文件名不包括扩展名
return "file-baogutang";
}
}

View File

@ -0,0 +1,195 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="NIKO.png">
<title>BAOGUTANG-FILE</title>
<style>
body {
font-family: "Microsoft YaHei", Arial, sans-serif;
background-color: #f7f8fa;
margin: 0;
padding: 0;
}
.container {
max-width: 1200px;
margin: 20px auto;
padding: 20px;
background: #fff;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: rgb(98, 210, 161);
margin-bottom: 20px;
}
h3 {
color: #444;
margin-bottom: 10px;
}
.section {
margin-bottom: 30px;
}
label {
font-weight: bold;
margin-bottom: 5px;
display: block;
}
input[type="file"], textarea, button {
width: 100%;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 3px;
font-size: 14px;
}
button {
background-color: rgb(98, 210, 161);
color: white;
cursor: pointer;
border: none;
}
button:hover {
background-color: rgba(98, 210, 161, 0.8);
}
textarea {
height: 150px;
resize: none;
}
.preview {
margin-top: 10px;
}
img, iframe {
max-width: 100%;
border: 1px solid #ddd;
border-radius: 3px;
}
iframe {
height: 80vh; /* PDF 预览高度设置为屏幕高度的 80% */
}
.download-link {
display: block;
margin-top: 10px;
color: rgb(98, 210, 161);
text-decoration: none;
font-weight: bold;
}
.download-link:hover {
text-decoration: underline;
}
.divider {
margin: 30px 0;
border-top: 1px solid #ddd;
}
</style>
</head>
<body>
<div class="container">
<h1>FILE-BAOGUTANG</h1>
<!-- 文件转 Base64 -->
<div class="section">
<h3>文件转 Base64</h3>
<label for="fileInput">选择文件</label>
<input type="file" id="fileInput" />
<button onclick="convertToBase64()">转换为 Base64</button>
<label for="base64Output">Base64 输出</label>
<textarea id="base64Output" readonly placeholder="Base64 输出"></textarea>
</div>
<div class="divider"></div>
<!-- Base64 转文件 -->
<div class="section">
<h3>Base64 转文件</h3>
<label for="base64Input">粘贴 Base64 字符串</label>
<textarea id="base64Input" placeholder="粘贴 Base64 字符串"></textarea>
<button onclick="convertToFile()">转换为文件并预览</button>
<div class="preview" id="previewArea"></div>
</div>
</div>
<script>
// 将文件转换为 Base64
function convertToBase64() {
const fileInput = document.getElementById('fileInput');
const base64Output = document.getElementById('base64Output');
if (!fileInput.files || fileInput.files.length === 0) {
alert('请选择一个文件!');
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = function (e) {
base64Output.value = e.target.result;
};
reader.onerror = function () {
alert('文件读取失败!');
};
reader.readAsDataURL(file); // 将文件读取为 Base64
}
// 将 Base64 转换为文件并预览
function convertToFile() {
const base64Input = document.getElementById('base64Input').value;
const previewArea = document.getElementById('previewArea');
if (!base64Input.trim()) {
alert('请提供有效的 Base64 字符串!');
return;
}
// 清空预览区域
previewArea.innerHTML = '';
// 创建文件链接
const [header, base64String] = base64Input.split(',');
const mimeType = header.match(/data:(.*);base64/)?.[1] || 'application/octet-stream';
const binary = atob(base64String || base64Input); // 解码 Base64
const arrayBuffer = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
arrayBuffer[i] = binary.charCodeAt(i);
}
const blob = new Blob([arrayBuffer], { type: mimeType });
const fileUrl = URL.createObjectURL(blob);
// 下载链接
const downloadLink = document.createElement('a');
downloadLink.href = fileUrl;
downloadLink.download = 'converted_file';
downloadLink.textContent = '点击下载文件';
downloadLink.className = 'download-link';
previewArea.appendChild(downloadLink);
// 根据文件类型生成预览
if (mimeType.startsWith('image/')) {
const img = document.createElement('img');
img.src = fileUrl;
previewArea.appendChild(img);
} else if (mimeType === 'application/pdf') {
const iframe = document.createElement('iframe');
iframe.src = fileUrl;
previewArea.appendChild(iframe);
} else if (mimeType.startsWith('text/')) {
const reader = new FileReader();
reader.onload = function (e) {
const textArea = document.createElement('textarea');
textArea.value = e.target.result;
previewArea.appendChild(textArea);
};
reader.readAsText(blob);
}
}
</script>
</body>
</html>