@RequiresPermissions("person:contract:list")
@GetMapping("/downLoadTemplateChang")
@ApiOperation("人员个税变更模板下载")
public ResultData<?> downLoadTemplateChang(HttpServletRequest request,HttpServletResponse response,String name) throws IOException {
service.downLoadTemplateChang(request,response,name);
return ResultData.result();
}
@Override
public void downLoadTemplateChang(HttpServletRequest request, HttpServletResponse response, String name) {
String filePath = null;
try {
filePath = getClass().getResource("/public/template/personnelChange_template.xlsx").getPath();
} catch (Exception e) {
e.printStackTrace();
throw new BaseException("下载异常,稍后重试");
}
FileUtils.goToDownload(response,filePath,name);
}
public static void goToDownload(HttpServletResponse response, String downUrl, String fileName) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
try {
String encodeFileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + new String(encodeFileName.getBytes("UTF-8"), "ISO8859-1"));
downUrl = URLDecoder.decode(downUrl, "UTF-8");
File file = new File(downUrl);
if (!file.exists()) {
response.sendError(404, "File not found!");
return;
}
long fileLength = file.length();
response.setHeader("Content-Length", String.valueOf(fileLength));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
<script type="text/javascript">
function download() {
var name="人员个税变更模板.xlsx"
var oReq = new XMLHttpRequest();
oReq.open("GET", "/individualTaxChange/downLoadTemplateChang?name="+name, true);
oReq.responseType = "blob";
oReq.onload = function (oEvent) {
var content = oReq.response;
var elink = document.createElement('a');
elink.download = name;
elink.style.display = 'none';
var blob = new Blob([content]);
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
};
oReq.send();
}
</script>
<div id="show_div" style="display: none">
<form class="form-horizontal m-t" id="shoutForm" enctype="multipart/form-data" novalidate="novalidate">
<div class="form-group">
<button onclick="download()" type="button" class="btn btn-outline btn-success cens_item">下载导入模板</button>
</div>
<input type="file" name="file" id="file_w" class="btn btn-sm btn-success btn_bg j-addGf" >
<div class="form-group">
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-3">
<button class="btn btn-primary" onclick="submitSc()" type="button" >提交</button>
</div>
</div>
</form>
</div>