zoukankan      html  css  js  c++  java
  • 根据一个oss的pdf文件的 地址转换成一个File文件

    /**
     * 根据文件地址返回 File
     * @param url 地址
     * @return
     */
    public static File doGetDownload(String url) {
        String separator=File.separator;
        String dir="ExistingEvidence/"+separator+"downLoad/";
        String fileName=System.currentTimeMillis()+".pdf";
        String path = dir+fileName;
        HttpClient httpClient = null;
        HttpGet httpPost = null;
        try {
            httpClient = new SSLClient();
            httpPost = new HttpGet(url);
            ProxyHttpClient.getProxyHttpClient(httpClient);
            HttpResponse response = httpClient.execute(httpPost);
            if (response == null) {
                throw new RuntimeException("HttpResponse is null.");
            }
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (null == entity) {
                    throw new RuntimeException("HttpEntity is null.");
                }
                // 下载成功返回文件流
                if (!"application/zip".equals(response.getEntity().getContentType().getValue()) && !"application/pdf".equals(response.getEntity().getContentType().getValue())) {
                    // 下载失败返回json格式报文
                    return null;
                }
                byte[] result = EntityUtils.toByteArray(response.getEntity());
                BufferedOutputStream bw = null;
                try {
                    File f = new File(path); // 创建文件对象
                    if (f.exists()) { // 重复时候替换掉
                        f.delete();
                    }
                    if (!f.getParentFile().exists()) { // 创建文件路径
                        f.getParentFile().mkdirs();
                    }
                    bw = new BufferedOutputStream(new FileOutputStream(path));// 写入文件
                    bw.write(result);
                    return f;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    try {
                        if (bw != null) {
                            bw.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                throw new RuntimeException("connect fail. http_status:" + response.getStatusLine().getStatusCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally {
            try {
                httpClient.getConnectionManager().shutdown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

     

  • 相关阅读:
    证券创新之翼 阿里金融云
    通过改变计算机策略来解决“只能通过Chrome网上应用商店安装该程序”的方法及模版文件下载
    VMware Workstation pro 12下载以及序列号
    Centos7下配置Tomcat7以指定(非root)身份运行
    apache2: Could not reliably determine the server's fully qualified domain name
    apache使用ssl数字证书
    苹果远程控制
    excel 组及分级显示制作教程
    Apache 性能优化
    Pigs and chickens
  • 原文地址:https://www.cnblogs.com/gjq1126-web/p/11207376.html
Copyright © 2011-2022 走看看