解决预览pdf不能下载的问题

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();
				}
			}
		}
	}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值