zoukankan      html  css  js  c++  java
  • http接口返回文件 服务端和客户端

    服务端:

        @RequestMapping(value = "/lsj", method = {RequestMethod.POST, RequestMethod.GET}, produces = {"text/plain;charset=UTF-8"})
        @ResponseBody
        public String searchLsjDzzz(String jsonStr,HttpServletResponse httpServletResponse) {
            byte[] dzzz = null;
            //下载PDF
            httpServletResponse.setHeader("Content-Disposition", "attachment;Filename=" + UUID.randomUUID()+".pdf");
            OutputStream outputStream = httpServletResponse.getOutputStream();
            outputStream.write(dzzz);
            outputStream.close();
    
            return "";
        }

    客户端:

        public byte[] requestByGetToByte(String url,String charset){
            if (url == null || "".equals(url))
            {
                log.error("request url is empty.");
                return null;
            }
            HttpClient httpClient = CustomHttpClient.GetHttpClient();
            HttpGet get = new HttpGet(url);
            get.setHeader("Content-Type", "text/html;charset="+charset);
            HttpResponse resp = null;
            try
            {
                // 发起交易
                resp = httpClient.execute(get);
                // 响应分析
                HttpEntity entity = resp.getEntity();
                byte[] bytes= EntityUtils.toByteArray(entity);
    
                return bytes;
            } catch (ConnectTimeoutException cte){
                log.error(cte.getMessage(), cte);
                return null;
            } catch (SocketTimeoutException cte){
                log.error(cte.getMessage(), cte);
                return null;
            } catch (Exception e){
                log.error(e.getMessage(), e);
                return null;
            }finally{
                try {
                    if(resp!=null){
                        resp.getEntity().getContent().close();
                    }
                } catch (IllegalStateException e) {
                    log.error(e.getMessage(), e);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
  • 相关阅读:
    一个漂亮的lazarus做的pagecontrol
    预测,阿里盒子必将失败!
    sex在软件开发中的运用--SIX技术
    糟糕的@@identity,SCOPE_IDENTITY ,IDENT_CURRENT
    Delphi、Lazarus保留字、关键字详解
    糟糕的界面设计
    Firebird存储过程--更加人性化的设计
    lazarus的动态方法和虚拟方法
    用户行为导向的交互设计
    Javascript的一个怪现象
  • 原文地址:https://www.cnblogs.com/zuferj115/p/7580664.html
Copyright © 2011-2022 走看看