- public class WordView implements View {
- public String contentType = "application/msword";
- private String fileName = "unknow.doc";
- private InputStream istream;
- public String getContentType() {
- return contentType;
- }
- public WordView(String fileName, InputStream istream){
- this.fileName = fileName;
- this.istream = istream;
- }
- /**
- *Spring View的扩展实现
- *@see View
- */
- public void render(Map<String, ?> arg0, HttpServletRequest arg1,
- HttpServletResponse response) throws Exception {
- response.addHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
- //response.setContentLength(istream.available());
- response.setContentType(contentType);
- POIFSFileSystem poifs = new POIFSFileSystem();
- DirectoryEntry directory = poifs.getRoot();
- directory.createDocument("WordDocument", this.istream);
- poifs.writeFilesystem(response.getOutputStream());
- response.getOutputStream().close();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2212201411380.html
来源: http://www.codesnippet.cn/detail/2212201411380.html