当前位置:网站首页>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();
}
}
}
}边栏推荐
- Data structures and differences between MySQL InnoDB engine and MyISAM
- Successful experience in postgraduate entrance examination for MTI master of English translation major of Beijing University of science and technology in 2023
- Gorm advanced query
- 搭建一个点歌QQ机器人,另外还能看美女
- 2021-04-27 classes and objects
- 2021-05-11 instanceof and type conversion
- Golang quick start (2)
- Different methods of PivotTable in SQL tutorial
- Go string comparison
- 构建信创产业生态,移动云立足全栈自主创新连放大招
猜你喜欢
随机推荐
几款实用软件分享
SQL教程之 5 个必须知道的用于操作日期的 SQL 函数
2021-04-27 classes and objects
Shengshihaotong enables high-quality development with industrial Digitalization
2021-04-16 recursion
NOI OJ 1.4 05:整数大小比较 C语言
Personal blog system graduation project opening report
Is IPv6 faster than IPv4?
Dr. Sun Jian was commemorated at the CVPR conference. The best student thesis was awarded to Tongji Ali. Lifeifei won the huangxutao Memorial Award
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
mysql innodb 的 redo log buffer 中未 commit 的事务持久化到 redo log 后,万一事务 rollback 了怎么办?redo log 怎么处理这个事务操作?
Golang 快速上手 (3)
云原生数据库-Amazon RDS
2021-05-11 instanceof and type conversion
2021-05-07封装 继承 super this
Lying trough, the most amazing paper artifact!
Unable to enter the system normally, press F8 to select other items to try to enter
Mathematical analysis_ Notes_ Chapter 2: real and plural numbers
【第23天】给定一个长度为 n 的数组,将元素 X 插入数组指定的位置 | 数组插入操作4
How to solve the problem that easycvr does not display the interface when RTMP streaming is used?









