}
4. (1,2,3步骤是把文件拉到本地,如果说在界面想出现下载界面的话可以发送ajax请求。下面是我写的,仅供参考)
$.ajax({
type: 'POST',
dataType: 'json',
url: "/device/exportLog",
data: { 'deviceSn': deviceSn } ,
timeout: 10000,
success: function ( data ) {
if(data.status){
Messenger().post({
message: data.info,
type: 'success',
showCloseButton: true
}) ;
var FileName = deviceSn.toLowerCase()+".zip";
var url = window.location.protocol + "//" + window.location.host + "/device/deviceLogdownFromHttp?fileName="+FileName;
window.location.href = url;
}else{
Messenger().post({
message: data.info,
type: 'error',
showCloseButton: true
}) ;
}
}
});
5.后台:
/**
* 下载zip文件
* */
@RequestMapping("/deviceLogdownFromHttp")
public void deviceLogdownFromHttp(HttpServletRequest request, HttpServletResponse response)throws Exception{
String fileName = request.getParameter("fileName");
try{
String resultPath =PropertyUtil.getProperty("tasksave.filePath")+"/"+fileName;// "E://AllTxt//zqzipzipzipzipzip.rar";
String zipName = resultPath.substring(0,resultPath.lastIndexOf(".")) + ".zip";
File zipFile = new File(zipName);
//读写zip文件
InputStream fis = new BufferedInputStream(new FileInputStream(zipFile));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
String targetName = zipName.substring(zipName.lastIndexOf("/")+1);
targetName = new String(targetName.getBytes("UTF-8"), "ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + targetName + "\"");
response.addHeader("Content-lenth", "" + buffer.length);
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer);
os.flush();
os.close();
response.flushBuffer();
String cc=fileName.substring(0,fileName.lastIndexOf(".")).toUpperCase();
sysLogMapper.deleteDownloadLogTaskByDeviceSn(fileName.substring(0,fileName.lastIndexOf(".")).toUpperCase());
}catch(Exception e){
e.printStackTrace();
}
}
6.(4,5步骤仅供参考,每个人写的不一样。)
主要是:
var FileName = deviceSn.toLowerCase()+".zip";
var url = window.location.protocol + "//" + window.location.host + "/device/deviceLogdownFromHttp?fileName="+FileName;
window.location.href = url;(前台)
(后台):deviceLogdownFromHttp这个方法