springmvc获取项目当前绝对路径,进行下载文件

<pre name="code" class="java">service层
@Autowiredprivate 
HttpServletRequest request;
String path = request.getServletContext().getRealPath("");//获取项目动态绝对路径

 
 
 
 
/**
* 下载压缩文件
* @param path request.getServletContext().getRealPath("")
* @param zipName 压缩文件名称
* @return
* @throws Exception
*/
public ResponseEntity<byte[]> download(String path, String zipName) throws Exception {
HttpHeaders headers = new HttpHeaders();
String filePath = null;
filePath = path + "/resources/excelzip/" + zipName + ".zip";
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", new String("知识类别.zip".getBytes("UTF-8"), "iso-8859-1"));
return (new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(filePath)), headers, HttpStatus.CREATED));
}

页面点击

<a href="./downloadFile/download" >下载</a>

也可以在JS中执行了某操作之后,自动调用下载功能

<script type="text/javascript">  
function xiazai(){
$("#id").click();//jqury 点击方法
}
</script>




你可能感兴趣的:(springmvc获取项目当前绝对路径,进行下载文件)