1.獲取tomcat的路徑:
兩個獲得路徑的方法:
System.out.println(new File(System.getProperty("catalina.home")));
System.out.println(new File(System.getProperty("user.dir")));
可根據需要使用。當然,也有直接獲得項目的物理路徑的:
?ServletActionContext.getServletContext().getRealPath("/") + "/modules/download/batchvehicle.xlsx";
2.js直接調用action?
思路:1:ajax.2表單提交3.iframe重定向4.winow.location.href=""
3.j2ee下載文件:
說也奇怪,以前全是jsp時,用window.open(url) 就直接可以了。但現在加入框架,怎么做也做不出來了。
所以寫在action層了:
public void downLoadVehiclePattern(){
OutputStream ous = null;
InputStream tis= null;
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.reset();
String header = "attachment; filename=BatchVehicle.xlsx";
header = new String(header.getBytes(), "UTF-8");
response.setHeader("Content-disposition", header);
String excelPath = ServletActionContext.getServletContext().getRealPath("/") + "/modules/download/batchvehicle.xlsx";
File file = new File(excelPath);
ous = response.getOutputStream();
tis= new FileInputStream(file);
byte[] bufs = new ?byte[1024];
int ? len ? = ? 0;
while((len = tis.read(bufs)) != -1) {
ous.write(bufs,0,len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(ous != null) {
try {
ous.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(tis != null) {
try {
tis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
而且在前臺,由于某種原因,用href 不方便,只能用js
如果用window.open會生成一個新頁面,故用window.location.href來解決。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
