struts 1 如何实现下载,主要是那个页面上显示一些标题,然后我要传递什么参数给action才能实现下载?
例子文章
http://xuehongliang.iteye.com/blog/53901
Struts+Spring+Hibernate实现上传下载
http://java.itpub.net/post/20968/383857
把如下代码加入Action代码中,传递2个参数,一个为文件地址,一个为文件明,response就会将文件流输出。要点在于FileInputStream 和OutputStream 的操作上。
[code="java"]
String path = request.getParameter("path");// 物理文件地址
String file = request.getParameter("file");// 文件名
path = request.getRealPath(path);
String fileName = path + File.separatorChar + file;
String tmpFileName = file.toLowerCase();
String fileExt = "";
String CONTENT_TYPE = "application/octet-stream;charset=GBK";
boolean headFlag = true;
if (fileName.endsWith(".doc")) {
fileExt = "doc";
CONTENT_TYPE = "application/msword;charset=GBK";
// headFlag = false;
} else if (tmpFileName.endsWith(".ppt")) {
fileExt = "ppt";
} else if (tmpFileName.endsWith(".xls")) {
fileExt = "xls";
} else if (tmpFileName.endsWith(".mdb")) {
fileExt = "mdb";
} else if (tmpFileName.endsWith(".zip")) {
fileExt = "zip";
} else if (tmpFileName.endsWith(".mp3")) {
fileExt = "mp3";
} else if (tmpFileName.endsWith(".avi")) {
fileExt = "avi";
} else if (tmpFileName.endsWith(".rm")) {
fileExt = "rm";
} else if (tmpFileName.endsWith(".gz")) {
fileExt = "gz";
} else if (tmpFileName.endsWith(".htm")) {
fileExt = "htm";
headFlag = false;
CONTENT_TYPE = "text/html; charset=GBK";
} else if (tmpFileName.endsWith(".html")) {
fileExt = "html";
headFlag = false;
CONTENT_TYPE = "text/html; charset=GBK";
} else if (tmpFileName.endsWith(".txt")) {
fileExt = "txt";
headFlag = false;
CONTENT_TYPE = "text/html; charset=GBK";
} else if (tmpFileName.endsWith(".rar")) {
fileExt = "rar";
} else if (tmpFileName.endsWith(".jpg")) {
fileExt = "jpg";
} else if (tmpFileName.endsWith(".gif")) {
fileExt = "gif";
} else if (tmpFileName.endsWith(".iso")) {
fileExt = "iso";
} else {
fileExt = tmpFileName.substring(tmpFileName.lastIndexOf("."),
tmpFileName.length());
}
String header_file = tmpFileName;
try {
header_file = new String(header_file.getBytes("GBK"), "ISO-8859-1");
} catch (Exception ex) {
header_file = tmpFileName;
}
response.setContentType(CONTENT_TYPE);
if (headFlag) {
response.setHeader("Content-Disposition", "attachment;filename="
+ header_file);
}
try {
// 导出数据
FileInputStream fis = new FileInputStream(fileName);
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
int n = 0;
while ((n = fis.read(buffer)) > 0) {
os.write(buffer, 0, n);
}
fis.close();
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
[/code]
看你要下载什么了
正常来说流操作直接写入就足够了
偷懒的办法把要下载的文件路径传到页面,用FTP的方式也未尝不可,嘿嘿,办法都是人想出来的,只要满足客户需求, 怎么做都行。