日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

j2ee中下載文件,js調用action,及獲得tomcat的

系統 2135 0

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來解決。



j2ee中下載文件,js調用action,及獲得tomcat的物理路徑等知識點


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 饶阳县| 遵义市| 隆子县| 洛隆县| 娄底市| 星座| 黎川县| 洪江市| 罗江县| 信阳市| 鲁山县| 辉县市| 邢台市| 抚宁县| 扎囊县| 山东省| 双辽市| 芒康县| 汕尾市| 进贤县| 汉沽区| 威远县| 银川市| 绥宁县| 疏勒县| 岳阳县| 紫金县| 花莲县| 枞阳县| 泽库县| 正镶白旗| 金沙县| 台中市| 原阳县| 泰顺县| 汝州市| 长顺县| 枣庄市| 兰西县| 甘南县| 乌鲁木齐县|