1,返回pdf关键代码

    /**
* @todo
* @param
* @date 2019年3月8日
* @author yanan
*/
@RequestMapping("/getPdf")
public void getPdf(HttpServletRequest req, HttpServletResponse response)throws Exception {
response.setContentType("application/pdf"); // 设置返回内容格式
try {
String pdfPath="放置你的指定pdf文件路径";
// 判断该路径下的文件是否存在
File file = new File(pdfPath);
if (file.exists()) {
DataOutputStream temps = new DataOutputStream(response.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(pdfPath));
byte[] b = new byte[2048];
while ((in.read(b)) != -1) {
temps.write(b);
temps.flush();
}
in.close();
temps.close();
} else {
System.out.println(" 文件不存在!");
} } catch (Exception e) {
logger.info(e.getMessage());
}
}

2,返回word关键代码

    /**
* @todo 导出word
* @param
* @date 2019年3月8日
* @author yanan
*/
@RequestMapping(value="/exportWord",method=RequestMethod.POST)
public void exportWord(HttpServletResponse response,HttpServletRequest req) throws IOException{
response.setContentType("application/doc"); // 设置返回内容格式
response.setHeader("Content-disposition","attachment; filename="+new String("你的文件名称".getBytes("utf-8"), "8859_1"));
DataOutputStream temps = new DataOutputStream(response.getOutputStream());
temps.write("你的文件内容".getBytes());
temps.flush();
temps.close();
}

3,返回excel关键代码

    /**
* @todo 导出excel
* @param tableHtml 要导出的table表格示例:<table><tr><td>aaa</td></tr></table>
* @param fileName 要导出的文件名
* @date 2019年3月8日
* @author yanan
*/
@RequestMapping(value="/exportExcel",method=RequestMethod.POST)
public void export(HttpServletResponse response,String tableHtml,String fileName,HttpServletRequest req) throws IOException{
String cssStyle = req.getParameter("cssStyle");//样式
cssStyle=cssStyle==null?"":cssStyle;
exportDataExcel(response, tableHtml, fileName,cssStyle);
}
private static void exportDataExcel(HttpServletResponse response, String tableHtml, String fileName,String cssStyle) throws IOException {
response.setCharacterEncoding("UTF-8");
//返回头设置文件名。Content-Disposition参数本来是为了在客户端另存文件时提供一个建议的文件名,但是考虑到安全的原因,就从规范中去掉了这个参数。
//但是由于很多浏览器已经能够支持这个参数,所以只是在规范文档中列出,但是要注意这个不是HTTP/1.1的标准参数。
//若不用java.net.URLEncoder.encode,则中文文件名乱码无法显示(英文部分不影响)
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName+".xls", "UTF-8"));
//返回内容为excel
response.setContentType("application/ms-excel;charset=UTF-8");
PrintWriter out = response.getWriter();;
tableHtml=String.format("<style type='text/css'>%s</style>%s", cssStyle, tableHtml);
out.print(tableHtml);
out.close();
}

利用poi导出,移步我之前的文章@java利用poi生成/读取excel表格

jsp请求java返回pdf、excel与word的更多相关文章

  1. 12、借助Jacob实现Java打印报表(Excel、Word)

    12.使用Jacob来处理文档 Word或Excel程序是以一种COM组件形式存在的.如果能够在Java中调用相应组件,便能使用它的方法来获取文档中的文本信息.Jacob是一个JAVA到微软的COM接 ...

  2. java将pdf文件转为word

    import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.Output ...

  3. Java 将PDF/XPS转为Word/html /SVG/PS/PCL/PNG、PDF和XPS互转(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了接口PdfConvertApi通过convert()方法将PDF文档以及XPS文档转为指定文档格式,如转PDF为Word(支持Docx.Doc). ...

  4. 从jsp到java文件再返回到前台页面的过程

    客户端请求jsp页面总共分为三个阶段: <%@ page language="java" contentType="text/html; charset=utf-8 ...

  5. .net mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法

    FlexPaper插件可以实现在浏览器中在线预览pdf,word,excel等. 在网上看到很多关于这个插件实现预览的技术,但是很难做到word和excel在线预览. pdf很好实现. 首先下载相关的 ...

  6. Java实现PDF和Excel生成和数据动态插入以及导出

    一.序言 Excel.PDF的导出.导入是我们工作中经常遇到的一个问题,刚好今天公司业务遇到了这个问题,顺便记个笔记以防下次遇到相同的问题而束手无策. 公司有这么两个需求: 需求一.给了一个表单,让把 ...

  7. JAVA实现office文档(word、excel、ppt等)、压缩包在线预览,支持禁止下载功能、支持PC和手机

    我们使用的是永中的第三方服务.支持直接转换文档的线上地址,也可以直接把文档上传到官方服务器上 官方文档地址:https://www.yozodcs.com/page/help.html#link152 ...

  8. java使用poi读取ppt文件和poi读取excel、word示例

    java使用poi读取ppt文件和poi读取excel.word示例 http://www.jb51.net/article/48092.htm

  9. Java中用Apache POI生成excel和word文档

    概述: 近期在做项目的过程中遇到了excel的数据导出和word的图文表报告的导出功能.最后决定用Apache POI来完毕该项功能.本文就项目实现过程中的一些思路与代码与大家共享.同一时候.也作为自 ...

随机推荐

  1. Egret飞行模拟-开发记录03-LoadingUI界面

    一.非EUI方式 1.LoadingUI里的代码.class LoadingUI extends egret.Sprite implements RES.PromiseTaskReporter { p ...

  2. 去除 ServiceStack.Redis 的6000次限制。

    方法一. 下载 https://github.com/ServiceStack/ServiceStack.Text 修改LicenseUtils.cs文件中的AssertValidUsage var ...

  3. udt通信java

    udt协议是什么? 简单的是udp重发 经过上次的修正,重新测试,修复,测试各种环境,再次查找出源码错误,重新修正 修正内容在git中的修正说明中 同时针对之后的应用,对封装的代码也做了修改和重构 代 ...

  4. spring事务学习笔记

    事务管理器 default使用数据库默认的隔离级别

  5. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  6. Linux 打包压缩与搜索命令

    1.tar 用于对文件进行打包压缩或解压,格式为tar[选项][文件],-f参数必须放到参数最后一位 tar -czvf etc.tar.gz /etc tar参数及作用 参数 作用 -c 创建压缩文 ...

  7. 5. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    解决方案,见  https://www.jianshu.com/p/836d455663da

  8. 关于前段JS代码报错问题的解决方法

    最近接手别人的一个项目,项目导入到Myeclipse中,JS代码一直报错,说missing semicolon.该错误是Myeclipse在检查JS代码的过程出现的. 后来经过检查,发现JS代码本身没 ...

  9. TypeError: Fetch argument 0 has invalid type <type 'int'>, must be a string or Tensor. (Can not convert a int into a Tensor or Operation.)

    6月5日的時候,修改dilated_seg.py(使用tensorflow)出現了報錯: TypeError: Fetch argument 0 has invalid type <type ' ...

  10. mysql脚本转h2

    注意事项:转的时候需要 脚本中不能包含utf8mb4格式