1. Excel文件使用Http服务实现下载
1.1. 在web.xml文件中注册新建的Servlet
<!-- ExcelDownloadServlet -->
<servlet>
<servlet-name>excelDownloadServlet</servlet-name>
<servlet-class>
com.jerry.support.ExcelDownloadServlet
</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>excelDownloadServlet</servlet-name>
<url-pattern>/excelDownloadServlet</url-pattern>
</servlet-mapping>
1.2. 创建ExcelDownloadServlet.java类,在容器中查找相应service服务,使用service服务去完成预定的任务
public class ExcelDownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
IExcelService iEexcelService = ServiceLocator.getInstance().getService("iEexcelService");
response.reset(); // 重置response
response.setContentType("application/msexcel;charset=\"UTF-8\"");
response.setHeader("Content-disposition","attachment; filename=IndexModelExcel.xls");
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
String path = iEexcelService.downloadTemplate(request);
PathUtils utils = new PathUtils();
File file = new File(utils.getWebRoot() + path);
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
while (-1 != in.read(buff)) {
out.write(buff);
buff = new byte[1024]; // 需要扩充字符数组
}
try {
if (null != in)
in.close();
if (null != out)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
response.flushBuffer();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
this.doGet(request, response);
}
}
request.getRequestURL():http://200.200.202.189:8081/project /excelDownloadServlet
request.getContextPath():/project
url: http://200.200.202.189:8081/visu/IndexExcel.xls
1.3.Web工程路径Utils
public class PathUtils {
/**
* 获取工程类路径(绝对路径)
*
* @return
*/
public String getWebClassesPath() {
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
return path;
}
/**
* 获取WEB-INF文件夹路径(绝对路径)
*
* @return
* @throws IllegalAccessException
*/
public String getWebInfPath() throws IllegalAccessException{
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(0, path.indexOf("WEB-INF")+8);
} else {
throw new IllegalAccessException("路径获取错误");
}
return path;
}
/**
* 获取WebRoot文件夹路径(绝对路径)
*
* @return
* @throws IllegalAccessException
*/
public String getWebRoot() throws IllegalAccessException{
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(0, path.indexOf("WEB-INF/classes"));
} else {
throw new IllegalAccessException("路径获取错误");
}
return path;
}
public static void main(String[] args) throws Exception {
PathUtils utils = new PathUtils();
System.out.println("1、" + utils.getWebClassesPath());
System.out.println("2、" + utils.getWebInfPath());
System.out.println("3、" + utils.getWebRoot());
}
}
输出:1、/E:/Workspaces/MyEclipse/project/WebRoot/WEB-INF/classes/
2、/E:/Workspaces/MyEclipse/project/WebRoot/WEB-INF/
3、/E:/Workspaces/MyEclipse/project/WebRoot/
1.4. 浏览器远程调用servlet在服务器端生成Excel文件,发生乱码及无法打开Excel文件的问题是因为在生成Excel文件时没有指定文件的编码格式,采用UTF-8的格式
File file = new File(realPath);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(excelXml);
osw.flush();
fos.close();