j2ee中下载文件,js调用action,及获得tomcat的物理路径等知识点
来源:互联网 发布:开淘宝店货源从哪里来 编辑:程序博客网 时间:2024/06/10 17:23
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的物理路径等知识点
- tomcat 中下载文件
- Windows中下载文件时的问题
- delphi程序中下载文件的问题
- .net中下载文件的方法
- .net中下载文件的方法
- .net中下载文件的方法
- .net中下载文件的方法
- java web中下载文件的问题
- ASP.NET中下载文件的方法
- spring中下载文件
- Form中下载文件
- JSP中下载文件
- ASP.NET中下载文件
- 从链接中下载文件
- Spring MVC中下载文件
- 微信中下载文件
- 从url中下载文件
- Android中如何做到Service被关闭后又自动启动
- poj 1962 Corporative Network
- Oracle复习(一)
- 高质量C++编程_第7章_内存管理(2)
- SecureCRT登录主机慢
- j2ee中下载文件,js调用action,及获得tomcat的物理路径等知识点
- 日期控件
- 在衡量web用程序的性能时,哪些性能指标是比较重要的?
- 问题的提出方式。(【先】说出最关键的【结果】,然后再描述细节)
- android退出提示
- C++(一)拷贝构造函数栈溢出的原因
- DoG (Difference of Gaussian)角点检测
- Dlink无线加密方式导致的笔记本掉线
- Git入门教程