1.获取XML数据
数据是这样获取的:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.daijia.soft.hospital.struts.action;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.daijia.soft.hospital.struts.form.DataMapForm;
import com.daijia.soft.hospital.vo.AttachVo;
/**
* MyEclipse Struts Creation date: 06-28-2008
*
* XDoclet definition:
*
* @struts.action path="/attachAction" name="dataMapForm" parameter="method"
* scope="request" validate="true"
*/
public class AttachAction extends BaseAction {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward queryByProtectId(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
DataMapForm dataMapForm = (DataMapForm) form;// TODO Auto-generated
// method stub
response.setContentType("text/xml;charset=utf-8");// 返回XML类型数据
response.setHeader("pragma", "no-cache");
response.setHeader("cache-control", "no-cache");
response.setDateHeader("expires", 0);
List<AttachVo> list = this.getHospitalService().getAttachsByProtectId(
dataMapForm.getInt("pid"));
StringBuffer xml = new StringBuffer("<attachs>");
for(AttachVo a : list){
xml.append("<attach old-file-name=""")
.append(a.getAoldname()).append(""" ")
.append("aid=""").append(a.getAid()).append(""" ")
.append("size=""").append(a.getAfilesize()).append(""" ")
.append("date=""").append(a.getAdatetime().toLocaleString()).append("""");
xml.append("></attach>");
}
xml.append("</attachs>");
if(logger.isDebugEnabled()){
logger.debug("附件:" + xml.toString());
}
PrintWriter out = response.getWriter();
out.println(xml.toString());
return null;
}
}
2.通过javascript显示文件下载列表
下载列表是这样生成的:
<script type="text/javascript">
<!--
var xmlhttp;
function displayAttach(pid, s){
//alert(s.offsetLeft);
var old = document.getElementById("divid");
if(old != null){
document.body.removeChild(old); //先删除附件层
}
var div = document.createElement("div");
var w = 400; //DIV的宽度
//获取s左边距和上边距
var olds = s;
var x = olds.offsetLeft;
var y = olds.offsetTop;
while(olds = olds.offsetParent){
x += olds.offsetLeft;
y += olds.offsetTop;
}
x = x - w; //往左边错开w像素
div.id = "divid";
div.style.position = "absolute";
div.style.backgroundColor = "white";
div.style.left = x;
div.style.top = y;
div.style.width = w;
div.className = "tdStyle3";
var table = document.createElement("table");
table.className = "tdStyle3";
table.style.width = "100%";
table.style.borderLeftColor = "blue";
table.style.borderLeftStyle = "solid";
table.style.borderLeftWidth = 1;
table.style.borderRightColor = "blue";
table.style.borderRightStyle = "solid";
table.style.borderRightWidth = 1;
table.style.borderTopColor = "blue";
table.style.borderTopStyle = "solid";
table.style.borderTopWidth = 1;
table.style.borderBottomColor = "blue";
table.style.borderBottomStyle = "solid";
table.style.borderBottomWidth = 1;
var tr = table.insertRow(0);
var titles = new Array("文件名", "文件大小", "上传日期");
for(var i = 0; i < titles.length; i ++){
var td1 = tr.insertCell(i);
if(i == 2){
td1.style.textAlign = "right";
var closea = document.createElement("a");
closea.innerHTML = "×";
closea.style.fontWeight = "bold";
closea.style.cursor = "pointer";
closea.onclick = function(){
var old2 = document.getElementById("divid");
if(old2 != null){
document.body.removeChild(old2); //先删除附件层
}
};
td1.appendChild(closea);
}
}
var tr = table.insertRow(1);
for(var i = 0; i < titles.length; i ++){
var td1 = tr.insertCell(i);
td1.innerHTML = titles[i];
td1.style.fontWeight = "bold";
}
//通过ajax得到附件数据
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
if(xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType("text/xml");
}
}else if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
xmlhttp.open("get", "attach.do?method=queryByProtectId&dataMap(pid)=" + pid);
xmlhttp.setRequestHeader("cache-control", "no-cache");
xmlhttp.send(null);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
var j = 0;
var xml = xmlhttp.responseXML;
var root = xml.documentElement;
var childs = root.childNodes;
if(childs.length == 0){
var trj = table.insertRow(2);
var tdj0 = trj.insertCell(0);
tdj0.innerHTML = "嘿嘿,没有附件";
}else{
for(j = 0; j < childs.length; j ++){
var trj = table.insertRow(j + 2); //在firefox中,添加新行时必须加行索引参数
var tdj0 = trj.insertCell(0);//在firefox中,添加新行时必须加列索引参数
var a = document.createElement("a");
a.innerHTML = childs.item(j).getAttribute("old-file-name");
a.href = "download.do?dataMap(aid)=" + childs.item(j).getAttribute("aid");
tdj0.appendChild(a);
var tdj1 = trj.insertCell(1);
tdj1.innerHTML = childs.item(j).getAttribute("size");
var tdj2 = trj.insertCell(2);
tdj2.innerHTML = childs.item(j).getAttribute("date");
}
}
}
}
};
div.appendChild(table);
document.body.appendChild(div);
}
//-->
</script>
3.下载选择的文件(使用Struts1.2.6自带的下载Action实现)
文件是这样下载的:
package com.daijia.soft.hospital.struts.action;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
import com.daijia.soft.hospital.service.IHospitalService;
import com.daijia.soft.hospital.struts.form.DataMapForm;
import com.daijia.soft.hospital.vo.AttachVo;
public class DownFileAction extends DownloadAction {
protected static Logger logger = Logger.getLogger("action");
private IHospitalService hospitalService;
public void setHospitalService(IHospitalService hospitalService) {
this.hospitalService = hospitalService;
}
public IHospitalService getHospitalService() {
return hospitalService;
}
@Override
protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm form,
HttpServletRequest arg2, HttpServletResponse response) throws Exception {
DataMapForm dataMapForm = (DataMapForm) form;
AttachVo vo = hospitalService.getRealFileName(dataMapForm.getInt("aid"));
response.reset();
//attachment:下载
//inline:在浏览器中显示
response.setHeader("Content-disposition", "attachment;filename=" + new String(vo.getAoldname().getBytes(), "iso-8859-1"));// 设置文件名称
//ResourceStreamInfo rsi = new ResourceStreamInfo("application/x-msdownload", this.getServlet().getServletContext(), "smtp.JPG");
FileStreamInfo rsi = new FileStreamInfo("application/x-msdownload", new File(vo.getAnewname()));
return rsi;
}
}
转自:http://www.blogjava.net/lzhidj/
分享到:
相关推荐
标题 "兼容ie,Firefox的文件上传" 涉及的核心知识点是实现跨浏览器的文件上传功能,特别是针对Internet Explorer(IE)和Firefox这两个主流浏览器的兼容性问题。在Web开发中,由于不同浏览器对HTML、CSS和JavaScript...
然而,非IE浏览器,如Chrome、Firefox等,它们可能不接受这种编码方式,而是更倾向于使用`UTF-8`编码并包含`filename*`字段来处理非ASCII字符的文件名。对于这种情况,你需要使用以下格式: ```http Content-...
- 需要针对不同浏览器进行兼容性处理,例如IE浏览器、Chrome浏览器和Firefox浏览器等。 - 兼容性处理示例: ```java String clientInfo = request.getHeader("User-Agent"); if (clientInfo != null && ...
这段代码展示了如何创建一个简单的文件下载服务,兼容不同类型的浏览器,如Microsoft Internet Explorer(IE)和Mozilla Firefox。以下是对这段代码的详细解析: 1. **CS代码**: - `DownFile` 方法接收两个参数:...
3、样式和内容分离:系统主体框架div css结构,遵循国际最新W3C网页设计标准,兼容IE系列、火狐等主流浏览器,内容和样式分离让网站风格可以轻松修改和更换,而不会导致内容和结构的破坏。 4、周密的安全策略和攻击...
4. **兼容性**:作为1.5.0版本,UEditor对多种浏览器和操作系统有很好的支持,包括IE6+、Firefox、Chrome、Safari等,并且此版本特别优化了GBK编码,适应ASP环境,适合各种服务器平台。 **2. ASP环境下的集成** ...
请注意,这种方法仅适用于IE浏览器,因为其他现代浏览器如Chrome、Firefox不支持ActiveXObject。 二、JS调用Word打印HTML 若想将HTML内容打印到Word,一种方法是先将HTML转换为Word文档,然后使用上述方法进行打印...
1.编辑器兼容Trident(IE)、Gecko(Firefox)、WebKit(Safari,Chrome)浏览器内核引擎 2.网站验证码改为点击输入框获取 3.RSS只读取审核通过的文章 4.RSS不分类读取时,自动判断文章相册视频的类别 5.后台TAG管理增加...
这行代码指定下载的文件名,并告诉浏览器这是一个附件,应当以下载形式处理。`filename`后的内容是下载文件时在用户浏览器中显示的文件名。 - `readfile("imgs/test_Zoom.jpg");` 这行代码将指定文件的内容读取到...
3、样式和内容分离:系统主体框架div css结构,遵循国际最新W3C网页设计标准,兼容IE系列、火狐等主流浏览器,内容和样式分离让网站风格可以轻松修改和更换,而不会导致内容和结构的破坏。 4、周密的安全策略和...
8、系统浏览器兼容性调整完毕,现在您可以在IE、firefox和遨游、腾讯TT等浏览器上正常使用本程序了 [2009-5-10] v3.1 beta1 1、部分功能ajax化 2、验证机制cookies化 3、界面UI更换 [2009-1-7] v 3.0 PACK3 ...
2. **兼容性广泛**:该库针对各种浏览器进行了优化,包括Chrome、Firefox、Safari、Edge和IE10+。 3. **简单API**:只用两行代码就可以实现文件保存,使得集成到项目中非常方便。 4. **类型支持**:支持Blob、File...
9.[改变]后台兼容模式提升到IE8模式 10.修复其他诸多小细节 2013年07月19日 V2.74更新包 1.[新增]后台引导页加入非IE浏览器提示,后台部分功能在非IE浏览器下可能没法使用 2.[修复]【紧急】纠正后台设皮肤目录存在...