spring mvc 通过流直接导出文本格式(excle,csv类似)
2015-11-06 18:05
573 查看
一边我们导出文件的时候,首先会先在服务器生成文件,然后再通过 (路径+文件名)的方式 来导出。。
现在呢, 我们不需要在服务器生成文件, 直接导出,应该怎样做呢?
看demo吧!
package com.broadtech.unicom.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.broadtech.unicom.base.BaseController;
@Controller
@RequestMapping("/ExportFileController.do")
public class ExportFileController extends BaseController{
@RequestMapping(params = "method=exportDownload")
public void exportDownload(String fileUrl, HttpServletRequest request, HttpServletResponse response) {
try {
if (fileUrl == null || "".equals(fileUrl)) {
response.getWriter().write("Parameter type is not correct!");
return;
}
fileUrl = URLDecoder.decode(fileUrl, "UTF-8");
String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
response.reset();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso8859-1"));
if (AbstractExport.EXPORT_SUFFIX_ZIP.equals(suffix)) {
response.setContentType("application/x-zip-compressed");
} else if (AbstractExport.EXPORT_SUFFIX_EXCEL.equals(suffix)) {
response.setContentType("application/vnd.ms-excel");
} else if (AbstractExport.EXPORT_SUFFIX_CSV.equals(suffix)) {
response.setContentType("application/oct-stream");
} else if (AbstractExport.EXPORT_SUFFIX_TEXT.equals(suffix)) {
response.setContentType("text/plain");
} else if (AbstractExport.EXPORT_SUFFIX_PNG.equals(suffix)) {
response.setContentType("image/x-png");
} else {
response.getWriter().write("Parameter type is not correct!");
return;
}
PrintWriter output = response.getWriter();
String outStream = "helloword";
for(int i = 0; i < 10; i ++){
output.println(outStream);
}
output.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class AbstractExport{
static String EXPORT_SUFFIX_ZIP = "zip";
static String EXPORT_SUFFIX_EXCEL = "xls";
static String EXPORT_SUFFIX_CSV = "csv";
static String EXPORT_SUFFIX_TEXT = "text";
static String EXPORT_SUFFIX_PNG = "png";
}
现在呢, 我们不需要在服务器生成文件, 直接导出,应该怎样做呢?
看demo吧!
package com.broadtech.unicom.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.broadtech.unicom.base.BaseController;
@Controller
@RequestMapping("/ExportFileController.do")
public class ExportFileController extends BaseController{
@RequestMapping(params = "method=exportDownload")
public void exportDownload(String fileUrl, HttpServletRequest request, HttpServletResponse response) {
try {
if (fileUrl == null || "".equals(fileUrl)) {
response.getWriter().write("Parameter type is not correct!");
return;
}
fileUrl = URLDecoder.decode(fileUrl, "UTF-8");
String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
response.reset();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso8859-1"));
if (AbstractExport.EXPORT_SUFFIX_ZIP.equals(suffix)) {
response.setContentType("application/x-zip-compressed");
} else if (AbstractExport.EXPORT_SUFFIX_EXCEL.equals(suffix)) {
response.setContentType("application/vnd.ms-excel");
} else if (AbstractExport.EXPORT_SUFFIX_CSV.equals(suffix)) {
response.setContentType("application/oct-stream");
} else if (AbstractExport.EXPORT_SUFFIX_TEXT.equals(suffix)) {
response.setContentType("text/plain");
} else if (AbstractExport.EXPORT_SUFFIX_PNG.equals(suffix)) {
response.setContentType("image/x-png");
} else {
response.getWriter().write("Parameter type is not correct!");
return;
}
PrintWriter output = response.getWriter();
String outStream = "helloword";
for(int i = 0; i < 10; i ++){
output.println(outStream);
}
output.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class AbstractExport{
static String EXPORT_SUFFIX_ZIP = "zip";
static String EXPORT_SUFFIX_EXCEL = "xls";
static String EXPORT_SUFFIX_CSV = "csv";
static String EXPORT_SUFFIX_TEXT = "text";
static String EXPORT_SUFFIX_PNG = "png";
}
相关文章推荐
- A fatal error has been detected by the Java Runtime Environment:
- 反射 私有方法与异常捕捉
- Spring的三种依赖注入方式
- 重写alert方法
- struts2文件下载
- Java中的向上转型和向下转型
- 从本地安装Eclipse的SVN插件详解
- Error:Internal error: (java.io.FileNotFoundException) D:\code\project\.idea\misc.xml (The sys
- spring mvc中处理多对多的同时存储
- Josephus环类问题,java实现
- 服务端工程师入门与进阶 Java 版
- JavaScirpt学习经历
- Java读取properties配置文件时,中文乱码解决方法
- spring ioc原理(看完后大家可以自己写一个spring)
- kettle crontab java: command not found
- myeclipse三个地方的java版本统一
- Spring 整合 Mina
- Java的IO流的操作和一些理解
- Javapns-sdk16-2.2.1多线程推送
- java 参数传递 空对象 null