zoukankan      html  css  js  c++  java
  • java 接口 文件传输

    调用接收端

        @ApiOperation(value = "文件请求展示方法")
        @RequestMapping(value = "/showFile", method = RequestMethod.GET)
        public void showFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
            //获取接口url
            String url = "http://127.0.0.1/a/b";
            //然后根据表名获取公司信息
            HttpPost httppost = new HttpPost(url);
            List<NameValuePair> params = new ArrayList<NameValuePair>();
    
            params.add(new BasicNameValuePair("comName", "111111"));
            HttpResponse httpResponse = null;
            HttpEntity httpEntity = null;
            try {
                httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
                RequestConfig requestConfig = RequestConfig.custom()  
                        .setConnectTimeout(30000).setConnectionRequestTimeout(30000)  
                        .setSocketTimeout(30000).build(); 
                httppost.setConfig(requestConfig);
                HttpClient httpclient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler()).build(); 
                httpResponse = httpclient.execute(httppost);
            }catch (Exception e1) {
                logger.error("----------------失败");
            }
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 请求正常
                try {
                    httpEntity = httpResponse.getEntity();
                    BufferedInputStream br = new BufferedInputStream(httpEntity.getContent());
                    byte[] buf = new byte[1024];
                    int len = 0;
                    response.reset(); // 非常重要
                    response.setContentType("application/pdf");
                    String fileName ="report.pdf";
                    try {
                        fileName = httpResponse.getAllHeaders()[5].getValue().split(";")[1].split("=")[1];
                    } catch (Exception e) {
                    }
                            
                    response.setHeader("Content-Disposition",
                            "inline; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
    
                    OutputStream out = response.getOutputStream();
                    while ((len = br.read(buf)) != -1)
                        out.write(buf, 0, len);
                    br.close();
                    out.flush();
    
                } catch (Exception e) {
                    logger.error("解析失败");
                }
            }else {
                logger.error("调用失败");
            }
            
        }

    文件存储方

       @ApiOperation(value = "文件支持接口")
        @RequestMapping(value = "/getReport", method = RequestMethod.GET)
        public Object getReport(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
            Map<String, Object> params = WebUtil.getParameterMap(request);
            String comName = (String) params.get("comName");
            File file = new File("C:\Users\Administrator\Desktop\滴滴电子发票.pdf");
            
            try {
                response.setContentType("application/pdf");
                response.setHeader("Content-Disposition",
                        "inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            
            try {
                ServletOutputStream pw = response.getOutputStream();
                BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
                byte[] by = new byte[(int) file.length()];
                while(br.read(by)!=-1){
                    pw.write(by);
                }
                pw.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            
            return null;
        }
  • 相关阅读:
    bag of words in c++
    中位数计算
    awk数据预处理
    收集主机OS相关数据
    Kernel trick----PRML读书笔记
    Error:java: 不再支持源选项 5。请使用 6 或更高版本。
    springboot mybatis日志级别
    java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
    docker部署Nacos集群
    WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.
  • 原文地址:https://www.cnblogs.com/xiufengd/p/11537445.html
Copyright © 2011-2022 走看看