如何将服务器端指定的文件(d:\A\lwl.xls)下载到客户端(弹出对话框选择本地欲存放的路径以及文件名)?
谢谢!
谢谢!
解决方案 »
- 请帮忙分析下手机支付网站是调用什么来产生密码加密的
- 求推荐web开发书籍 标题长长长~~~~~~~~~~~~~~~~~~~~~~~~··
- hibernate查询排序
- www.zuovip.com 的语音聊天 服务端 或 客户端 关键技术讨论
- 紧急求助,request.getParameterMap()怎么返回null,研究了一上午了!!!!!
- jsp中form的method(get) 传递参数问题
- 求《The JasperReports Ultimate Guide》的电子文档
- 关于jdk的问题??
- list合并问题
- javax.servlet.ServletException: java.sql.SQLException: [Microsoft][SQLServer 20
- 文本域验证的问题,^_^
- 怎么通过客户端查询语句正确获取服务器端信息?
import javax.servlet.http.*;
import java.io.*;
import java.lang.reflect.*;
import java.sql.*;/**
* 类描述:${describe} 生成时间: 2005-10-26 16:46:25
*
* @author user
* @version Revision
*/public class FileViewServlet extends HttpServlet { private String dsName = "OpenojSessionFactory"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String vo = request.getParameter("vo");
String pkg = request.getParameter("pkg");
String colum = request.getParameter("colum");
String type = request.getParameter("download"); if (vo == null || id == null || colum == null)
throw new ServletException("参数[vo,id,colum]必须全部指定,pkg默认为[com.gsbiz.vo],下载download默认为[true]~!");
if (pkg == null || pkg.length() == 0)
pkg = "com.gsbiz.vo";
if (type == null || type.length() == 0)
type = "true"; String clasz = pkg + "." + vo; try {
Class voclass = Class.forName(clasz);
Datastore ds = PersistenceFactory.create().getDatastore(dsName);
Object rvo = ds.findByPK(voclass, id); String methodName = "get" + colum.substring(0, 1).toUpperCase() + colum.substring(1);
Method method = voclass.getMethod(methodName, null);
Object file = method.invoke(rvo, null); if (file instanceof Blob || file instanceof Clob) { InputStream in = null;
if (file instanceof Blob) {
Blob blob = (Blob) file;
in = blob.getBinaryStream();
}
else if (file instanceof Clob) {
Clob clob = (Clob) file;
in = clob.getAsciiStream();
} //设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0); if (type.equalsIgnoreCase("true")) {
response.setContentType("application/x-download");
String methodName1 = "get" + colum.substring(0, 1).toUpperCase() + colum.substring(1) + "FileName";
Method method1 = voclass.getMethod(methodName1, null);
Object fileName = method1.invoke(rvo, null);
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
}
else {
response.setContentType("image/gif"); } ServletOutputStream outputStream = response.getOutputStream(); byte[] data = new byte[2048];
int size;
while ((size = in.read(data)) != -1) {
outputStream.write(data, 0, size);
}
in.close();
outputStream.flush();
outputStream.close();
}
}
catch (Exception e) {
e.printStackTrace();
} }
}
我用jspsmart组建暂时实现了。
先收藏!
非常感谢(踏月无痕)!