当前位置:网站首页>解决预览pdf不能下载的问题
解决预览pdf不能下载的问题
2022-06-23 10:07:00 【cfcoolya】
1.背景
最近在开发A系统归档的数据要传入B系统,在B系统生成PDF文件。之后要在A系统预览并下载。
2.问题

1.文件名不能回填
2.保存类型没有pdf
3.解决
/**
* 实现资产系统通过接口方式
* PDF预览
* @author chenf
*/
@RequestMapping(value = "/sign-process/queryFile", method = RequestMethod.GET)
public RestfulResult queryFile(String assetUrl,
HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
{
String param = assetUrl;
param = URLDecoder.decode(URLDecoder.decode(param, "utf-8"),"utf-8");
param = StringEscapeUtil.encode(param);
OutputStream out = null;
RestfulResult result = new RestfulResult();
try
{
out = response.getOutputStream();
// 执行业务
this.signAssetservice.getAssetFile(request,response,param,out);
return null;
} catch (Exception e)
{
log.setState("错误");
log.setDescribe(e.toString());
logManage.addLog(log);
result.setCode(RestfulResult.ERROR_CODE);
result.setMsg(log.getDescribe());
return result;
}finally
{
try
{
if(out != null)
{
out.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
@Override
public void getAssetFile(HttpServletRequest request,HttpServletResponse response,String param,OutputStream out) {
FileInputStream in = null;
String fileurl = null;
fileurl = param;
try {
File file = new File(fileurl);
String fileName = file.getName();
//中文兼容火狐浏览器
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
downShowName = "=?UTF-8?B?"+(new String(Base64.encode(fileName.getBytes("UTF-8")))) + "?=";
} else {
downShowName = java.net.URLEncoder.encode(fileName,"UTF-8");
}
response.setContentType("application/pdf");//告诉浏览器输出内容为pdf
response.setHeader("Content-Disposition", "inline;filename="+downShowName);//inline显示|attachment下载
in = new FileInputStream(file);
byte[] date = new byte[in.available()];//获取文件的大小
in.read(date);//以字节流的形式读取文件
out.write(date);
}catch (Exception e) {
throw new RuntimeException(e.toString());
}finally {
if(in!=null){
try
{
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}边栏推荐
- 2021-05-11 abstract class
- Gorm 高级查询
- How to pass values to onclick events in thymeleaf
- 2021-04-15
- The era of copilot free is over! The official version is 67 yuan / month, and the student party and the defenders of popular open source projects can prostitute for nothing
- Tencent tangdaosheng: practice "science and technology for the good" and promote sustainable social value innovation
- 必须知道的RPC内核细节(值得收藏)!!!
- SQL writing problem to calculate the ring ratio of the current month and the previous month
- 搭建一个点歌QQ机器人,另外还能看美女
- Personal blog system graduation project opening report
猜你喜欢

2021-05-07 constructor

Fill the pit for repvgg? In fact, it is the repoptimizer open source of repvgg2

Build the information and innovation industry ecology, and make mobile cloud based on the whole stack of independent innovation

开发者,你对云计算可能有些误解

Go 字符串比较

J. Med. Chem. | Release: a new drug design model for deep learning based on target structure

Shengshihaotong enables high-quality development with industrial Digitalization

Successful experience of postgraduate entrance examination in materials and Chemical Engineering (metal) of Beijing University of Aeronautics and Astronautics in 2023

Go unit test

谷贱伤农,薪贱伤码农!
随机推荐
基於STM32設計的寵物投喂器
sql编写问题,求出当月和上月的环比值
高性能算力中心 — InfiniBand — Overview
[day 23] given an array of length N, insert element x into the position specified by the array | array insertion operation 4
NFTs、Web3和元宇宙对数字营销意味着什么?
Bioinformatics | 基于相互作用神经网络的有效药物-靶标关联预测
STM32F1与STM32CubeIDE编程实例-红外寻迹传感器驱动
2021-05-07构造器
Spring recruitment interview experience summary (technical post)
Liujinhai, architect of zhongang Mining: lithium battery opens up a Xintiandi for fluorine chemical industry
【软件与系统安全】堆溢出
AI系统前沿动态第38期:谷歌已放弃TensorFlow?;训练大模型的四种GPU并行策略;LLVM之父:模块化设计决定AI前途
【第23天】给定一个长度为 n 的数组,将元素 X 插入数组指定的位置 | 数组插入操作4
Successful experience in postgraduate entrance examination for MTI master of English translation major of Beijing University of science and technology in 2023
2021-04-16方法重载 传参
2021-05-07 constructor
Is there anyone who plans to open source an industrial "seckill" system architecture?
几款实用软件分享
Unable to enter the system normally, press F8 to select other items to try to enter
Nuxt. Differences between JS spa and SSR