提交 2dcd005f 编写于 作者: H hml

默认的

上级 03b35aa6
...@@ -3,6 +3,7 @@ package com.hao.digitalsignature.controller; ...@@ -3,6 +3,7 @@ package com.hao.digitalsignature.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hao.digitalsignature.encryption.AES1; import com.hao.digitalsignature.encryption.AES1;
import com.hao.digitalsignature.encryption.AESmiyao; import com.hao.digitalsignature.encryption.AESmiyao;
import com.hao.digitalsignature.encryption.DownloadMsg;
import com.hao.digitalsignature.encryption.RSAEncrypt; import com.hao.digitalsignature.encryption.RSAEncrypt;
import com.hao.digitalsignature.entity.Files; import com.hao.digitalsignature.entity.Files;
import com.hao.digitalsignature.mapper.FileMapper; import com.hao.digitalsignature.mapper.FileMapper;
...@@ -13,13 +14,10 @@ import org.springframework.web.bind.annotation.RequestParam; ...@@ -13,13 +14,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException; import java.net.SocketException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.*;
...@@ -70,36 +68,7 @@ public class FileDealController { ...@@ -70,36 +68,7 @@ public class FileDealController {
// 得到要下载的文件名 // 得到要下载的文件名
fileName = fileName.substring(0,fileName.length()-1); fileName = fileName.substring(0,fileName.length()-1);
try { try {
//获取摘要
QueryWrapper<Files> wrapper = new QueryWrapper<>();
wrapper.eq("picture_realname", fileName.split("\\.")[0]);
Files files = fileMapper.selectOne(wrapper);
System.out.println("摘要:"+files.getDig().split(";")[2]);
List<String> dig=new ArrayList<String>(Arrays.asList(files.getDig().split(";")));
dig.add(files.getCreatetime());
String[] dig1=new String[dig.size()];
dig.toArray(dig1);
for(int i=0;i<dig1.length;i++)
System.out.println("正确签名和时间戳"+dig1[i]);
//生成AES密钥
String aesPassword =AESmiyao.getKey();
//AES加密
String encryContent= AES1.encryptAES(files.getDig().split(";")[2],aesPassword);
//对称加密后的签名
dig1[2]=encryContent;
for(int i=0;i<dig1.length;i++)
System.out.println("加密摘要的签名和时间戳"+dig1[i]);
//发送RSA
System.out.println(Arrays.toString(dig1));
String rsaMsg=new String();
for(int i=0;i<dig1.length;i++){
rsaMsg+=dig1[i];
rsaMsg+=";";
}
byte[] rsaEn=RSAEncrypt.RSAen(rsaMsg.substring(0,rsaMsg.length()-1));
//RSA解密
String rsaDe=RSAEncrypt.RSAde(rsaEn);
...@@ -123,8 +92,7 @@ public class FileDealController { ...@@ -123,8 +92,7 @@ public class FileDealController {
} }
// 处理文件名 // 处理文件名
String realname = fileName.substring(fileName.indexOf("_") + 1); String realname = fileName.substring(fileName.indexOf("_") + 1);
// 设置响应头,控制浏览器下载该文件
// response.setContentType("image/png");
response.setHeader("Content-Disposition", "attachment;filename=" response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(realname, "UTF-8")); + URLEncoder.encode(realname, "UTF-8"));
// 读取要下载的文件,保存到文件输入流 // 读取要下载的文件,保存到文件输入流
...@@ -150,4 +118,55 @@ public class FileDealController { ...@@ -150,4 +118,55 @@ public class FileDealController {
} }
} }
@RequestMapping(value = "/downloadMsg")
public void exportKtrAndKjb(@RequestBody String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
fileName = fileName.substring(0,fileName.length()-1);
//获取摘要
try {
QueryWrapper<Files> wrapper = new QueryWrapper<>();
wrapper.eq("picture_realname", fileName.split("\\.")[0]);
Files files = fileMapper.selectOne(wrapper);
System.out.println("文件对象:"+files);
System.out.println("摘要:"+files.getDig().split(";")[2]);
List<String> dig=new ArrayList<String>(Arrays.asList(files.getDig().split(";")));
dig.add(files.getCreatetime());
String[] dig1=new String[dig.size()];
dig.toArray(dig1);
for(int i=0;i<dig1.length;i++)
System.out.println("正确签名和时间戳"+dig1[i]);
//生成AES密钥
String aesPassword =AESmiyao.getKey();
//AES加密
String encryContent= AES1.encryptAES(files.getDig().split(";")[2],aesPassword);
//对称加密后的签名
dig1[2]=encryContent;
for(int i=0;i<dig1.length;i++)
System.out.println("加密摘要的签名和时间戳"+dig1[i]);
//发送RSA
System.out.println(Arrays.toString(dig1));
String rsaMsg=new String();
for(int i=0;i<dig1.length;i++){
rsaMsg+=dig1[i];
rsaMsg+=";";
}
System.out.println(rsaMsg.length());
byte[] rsaEnf=RSAEncrypt.RSAen(rsaMsg.substring(0,rsaMsg.length()/2));
byte[] rsaEns=RSAEncrypt.RSAen(rsaMsg.substring(rsaMsg.length()/2));
//RSA解密
String rsaDef=RSAEncrypt.RSAde(rsaEnf);
String rsaDes=RSAEncrypt.RSAde(rsaEns);
String recRes=rsaDef+rsaDes;
DownloadMsg.downloadByStringContent(request, response, fileName.split("\\.")[0], recRes);
}catch (Exception e) {
System.out.println("error");
}
}
} }
package com.hao.digitalsignature.encryption;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
public class DownloadMsg {
public static void downloadByStringContent(HttpServletRequest request,
HttpServletResponse response,
String fileName, String content)
throws IOException {
//设置向浏览器端传送的文件格式
response.setContentType("application/octet-stream;charset=utf-8");
response.setCharacterEncoding("utf-8");
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
// firefox浏览器
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
} else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
// IE浏览器
fileName = URLEncoder.encode(fileName, "UTF-8");
} else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
// 谷歌
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
BufferedInputStream inp = null;
OutputStream out = response.getOutputStream();
try {
inp = new BufferedInputStream(new ByteArrayInputStream(content.getBytes("utf-8")));
int len = 0;
byte[] buf = new byte[1024];
while ((len = inp.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inp != null) {
inp.close();
}
if (out != null) {
out.close();
}
}
}
}
spring.devtools.restart.enabled=true spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java spring.devtools.restart.additional-paths=src/main/java
server.port=8080 server.port=8081
#ϴļС #ϴļС
logging.logback.rollingpolicy.max-file-size=10MB logging.logback.rollingpolicy.max-file-size=10MB
#swagger #swagger
......
...@@ -224,7 +224,52 @@ ...@@ -224,7 +224,52 @@
} }
}); });
}) })
axios.post("/file/downloadMsg",f,).then((res)=>{
console.log(res)
// var url = (window.document.location.href+"img/"+res.config.data)
// var a = document.createElement("a");
// a.download = file.picture_name;
// a.href = URL.createObjectURL(blob);
// document.body.appendChild(a)
// a.click();
// URL.revokeObjectURL(a.href) // 释放URL 对象
// document.body.removeChild(a)
// var url = (window.document.location.href+"img/"+res.config.data)
// var that = this
// var fileName = file.picture_name
// this.convertUrlToBase64(url).then(function (base64) {
// var blob = that.convertBase64UrlToBlob(base64); // 转为blob对象
// // 下载
// if (that.myBrowser() == "IE") {
// window.navigator.msSaveBlob(blob, fileName + ".txt");
// } else if (that.myBrowser() == "FF") {
// window.location.href = url;
// } else {
// var a = document.createElement("a");
// a.download = fileName;
// a.href = URL.createObjectURL(blob);
// document.body.appendChild(a)
// a.click();
// URL.revokeObjectURL(a.href) // 释放URL 对象
// document.body.removeChild(a)
// }
// });
this.downloadFile(res.data,file.picture_name)
})
},
downloadFile(data,fileName){
// data为blob格式
var blob = new Blob([data]);
var downloadElement = document.createElement('a');
var href = window.URL.createObjectURL(blob);
downloadElement.href = href;
downloadElement.download = fileName;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(href);
}, },
// 转换为base64 // 转换为base64
convertUrlToBase64(url) { convertUrlToBase64(url) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册