文档库 最新最全的文档下载
当前位置:文档库 › 异地下载为xml

异地下载为xml

文件下载示例

引言

这里以下载一条数据记录为例,将记录对应的对象实例解析为xml文件并下载到本地。

准备页面

选中一条记录点击下载

主要内容:

function exportProcess(){

var action="<%=request.getContextPath() %>

/flow_define/processList.do?method=exportProcess&processId=" +processId;

window.location.href=action; //当前网页上新开一个指定了URL的网页

}

Action处理

主要内容:

String processId = request.getParameter("processId");

//根据主键先得到记录对象实例

Process processObj .....

byte[] data = objectXmlEncoderToByteArray(processObj);

public byte[] objectXmlEncoderToByteArray(Object obj)

throws IOException, Exception {

java.io.ByteArrayOutputStream bout = new ByteArrayOutputStream(); //将对象存到输出流中

xmlEncoder(bout,obj);

//将输出流转为byte字节

return bout.toByteArray();

}

private void xmlEncoder(OutputStream outputStream, Object obj)

throws IOException {

java.beans.XMLEncoder encoder = new XMLEncoder(outputStream);

//将对象序列化到流出流

encoder.writeObject(obj);

encoder.flush();

// 关闭序列化工具

encoder.close();

// 关闭输出流

outputStream.close();

}

//将要转入到哪个文件的文件名,子节数组传入下载执行页面

request.setAttribute("fileName", processObj.getName() + ".xml"); request.setAttribute("data", data);

return mapping.findForward("download"); //跳到下载执行页面

下载执行页面

所有内容:

<%

String fileName = (String) request.getAttribute("fileName");

byte[] data = (byte[]) request.getAttribute("data");

//设置下载页头

response.setContentType("application/xml;charset=GB2312"); response.addHeader("Content-Disposition", "attachment;filename="

+ new String(fileName.getBytes("GBK"), "ISO8859_1"));

response.getOutputStream().write(data); //下载输出流到目标地址response.getOutputStream().close();

%>

最终产生一个xml文件

相关文档