介绍:clob 是一种大文本数据类型,用于存储大量文本数据。
1,新建fileUtil类(根据个人需要),并实现下面方法
/**
* 二进制转换字符串(上传用)
* @param b
* @return
*/
public static String byte2hex(byte[] b){
StringBuffer sb = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
sb.append("0" + stmp);
} else {
sb.append(stmp);
}
}
return sb.toString();
}
/**
* 字符串转换二进制(下载用)
* @param str
* @return
*/
public static byte[] hex2byte(String str) {
if (str == null)
return null;
str = str.trim();
int len = str.length();
if (len == 0 || len % 2 == 1)
return null;
byte[] b = new byte[len / 2];
try {
for (int i = 0; i < str.length(); i += 2) {
b[i / 2] = (byte) Integer.decode("0X" + str.substring(i, i + 2)).intValue();
}
return b;
} catch (Exception e) {
return null;
}
}
2,Controller类中写上传方法
@PostMapping("/uploadFile")
public AjaxResult add(@RequestParam(value = "file", required = true) MultipartFile file, @RequestParam(value = "sxtgdbh", required = false) String sxtgdbh) throws IOException {
InputStream inputStream = file.getInputStream();
byte[] byteFile = FileCopyUtils.copyToByteArray(inputStream);
Order order = orderService.workOrderInfo(sxtgdbh);
Order.setSXtgdfj(FileUtil.byte2hex(byteFile));
Order.setFileName(file.getOriginalFilename());
orderService.workOrderUpdate(escalationWorkOrder);
return AjaxResult.success();
}
3,Controller类中写下载方法
@GetMapping("/preview")
public void getPdfData(String sxtgdbh, HttpServletRequest request, HttpServletResponse response){
Order workOrder = orderService.workOrderInfo(sxtgdbh);
ByteArrayInputStream bin=null;
OutputStream os = null;
try {
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=\"" + new String("aaa".getBytes ("utf-8"), "ISO8859-1" )+"docx"+ "\"");
os = response.getOutputStream();
bin = new ByteArrayInputStream(FileUtil.hex2byte(workOrder.getSXtgdfj()));
int len=0;
byte[] buffer=new byte[1024];
while((len=bin.read(buffer))>0){
os.write(buffer,0,len);
}
os.flush();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
4,HTML测试代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件下载</title>
</head>
<body>
<button onclick="downloadFile()">下载文件</button>
<script>
function downloadFile() {
// 创建一个XMLHttpRequest对象
var xhr = new XMLHttpRequest();
// 定义文件下载地址
var fileUrl = "http://localhost:10086/escalation/work/order/preview";
// 初始化请求
xhr.open('GET', fileUrl, true);
// 设置请求头
xhr.setRequestHeader('Authorization', '');
// 设置响应类型为Blob
xhr.responseType = 'blob';
// 监听请求状态变化
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
// 创建一个隐藏的<a>标签,用于下载文件
var a = document.createElement('a');
var blob = new Blob([xhr.response], {type: 'multipart/form-data'});
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = 'file_name.docx'; // 修正为实际的文件名
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
} else {
// 请求失败时的处理
console.error('文件下载失败');
}
}
};
// 发送请求
xhr.send();
}
</script>
</body>
</html>