当前位置:网站首页>Solve the problem that Preview PDF cannot be downloaded
Solve the problem that Preview PDF cannot be downloaded
2022-06-23 10:24:00 【cfcoolya】
1. background
It's been developed recently A The data archived by the system should be transferred into B System , stay B System generated PDF file . After that A The system previews and downloads .
2. problem

1. File names cannot be backfilled
2. Save type no pdf
3. solve
/**
* Implement the asset system through the interface mode
* PDF preview
* @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();
// Executive business
this.signAssetservice.getAssetFile(request,response,param,out);
return null;
} catch (Exception e)
{
log.setState(" error ");
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();
// Chinese compatible Firefox browser
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");// Tell the browser that the output is pdf
response.setHeader("Content-Disposition", "inline;filename="+downShowName);//inline Show |attachment download
in = new FileInputStream(file);
byte[] date = new byte[in.available()];// Get file size
in.read(date);// Read the file as a byte stream
out.write(date);
}catch (Exception e) {
throw new RuntimeException(e.toString());
}finally {
if(in!=null){
try
{
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}边栏推荐
猜你喜欢

Dr. Sun Jian was commemorated at the CVPR conference. The best student thesis was awarded to Tongji Ali. Lifeifei won the huangxutao Memorial Award

Three implementation methods of distributed lock

云原生数据库-Amazon RDS

sql编写问题,求出当月和上月的环比值

STM32F1与STM32CubeIDE编程实例-红外寻迹传感器驱动

Cloud native database Amazon RDS

Gorm advanced query

Is IPv6 faster than IPv4?

File IO (1)

基于STM32设计的宠物投喂器
随机推荐
JS教程之 什么是 JSX?为什么我们需要它?
[software and system security] heap overflow
2021-04-16 method overload parameter transfer
春招面试经验汇总(技术岗)
Is there anyone who plans to open source an industrial "seckill" system architecture?
135,137,138,445禁用导致无法远程连接数据库
Is IPv6 faster than IPv4?
同花顺是炒股的么?在线开户安全么?
NiO example
IPv6 的速度比 IPv4 更快?
NOI OJ 1.4 03:奇偶数判断 C语言
NOI OJ 1.4 04:奇偶ASCII值判断 C语言
Golang quick start (3)
文献综述怎么写 ,一直没头绪写不出来怎么办?
Multithreaded exercises
Spring recruitment interview experience summary (technical post)
Set up a QQ robot for ordering songs, and watch beautiful women
thymeleaf中如何给onclick事件传值的方法
mysql中innodb下的redo log什么时候开始执行check point落盘的?
解决audio自动播放无效问题