spring MVC+ extjs 导出文件 总是返回数据 不是文件 急等

gaojiaoedu 2011-03-19 05:39:31
spring MVC+ extjs 导出文件 总是返回数据 不是文件 急等
...全文
177 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaojiaoedu 2011-03-19
  • 打赏
  • 举报
回复
相应结果
firefox 控制台相应结果
http://localhost:8080/...200 OK
8.39s
ext-base.js (第 7 行)
参数HeadersPostPut响应缓存HTML

��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�0,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�1,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�2,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�3,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�4,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�5,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�6,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�7,��ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�����ò���CVS�ļ�8
gaojiaoedu 2011-03-19
  • 打赏
  • 举报
回复
extjs部分

// 导出 请求function
function submitExportData() {
Ext.Ajax.request({
url : "../../../../service/sec/operlog/exportOperLog",
method: "post",
autoAbort : true,
xmlData : getExportDataXML(),
// 请求成功时调用
success : function(resp, opts) {
Ext.Msg.alert("导出提示", "导出成功");
},
// 请求失败时调用
failure : function(resp, opts) {
Ext.Msg.alert("导出提示", "导出失败");
}
});
}

gaojiaoedu 2011-03-19
  • 打赏
  • 举报
回复

public void exportOperLog(@RequestBody BasicRequest form, HttpServletResponse response)
throws IOException
{
logger.debug("[security] exportOperLog() enter");

//设置响应编码
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
try
{
response.setHeader("Content-Disposition", "attachment; filename=" + "测试下载.cvs");
}
catch (Exception e)
{
logger.error("security exportOperLog() throws Exception" + e);
}

// 所下载的文件路径
File downFile = new File("c:/2.csv");
ServletOutputStream servletOutputStream = null;
InputStream inputStream = null;

//如果是文件,存在
if (downFile.isFile())
{
try
{
// 实例化文件输入流对象
inputStream = new FileInputStream(downFile);
servletOutputStream = response.getOutputStream();

// 读取数量
int iByteSum = 0;

// 读取字节数
int iByteRead = 0;

// 缓存 1024
byte[] bufferByte = new byte[1024];

//循环读取数据
while ((iByteRead = inputStream.read(bufferByte)) != -1)
{
iByteSum += iByteRead;
servletOutputStream.write(bufferByte, 0, iByteRead);
}
}
catch (FileNotFoundException e)
{
logger.error(" downLoad() inputStream appear FileNotFoundException" + e.toString());
}
finally
{
try
{
//执行完毕关闭servletOutputStream
if (servletOutputStream != null)
{
// 关闭文件输出流
servletOutputStream.close();

//释放资源
servletOutputStream = null;
}
}
catch (IOException e)
{
logger.error(" downLoad() Close inputStream appear IOException" + e.toString());
}
try
{
//执行完毕关闭inputStream
if (inputStream != null)
{
// 关闭输入流
inputStream.close();

//释放资源
inputStream = null;
}
}
catch (IOException e)
{
logger.error(" downLoad() Close inputStream appear IOException" + e.toString());
}
}

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧