zoukankan      html  css  js  c++  java
  • Java 通过URL进行远程文件下载

    @Service
    public class FileService {
    private static Logger LOGGER = LoggerFactory.getLogger(FileService.class);
    
    @Autowired
    private HttpService httpService;
    @Autowired
    private CloseableHttpClient httpClient;
    @Autowired
    private RequestConfig config;
    
    public ByteArrayInputStream download2(String url) {
    CloseableHttpResponse response = null;
    try {
    url = url.replaceAll(" ", "%20");
    LOGGER.info("download fileUrl:{}",url);
    URIBuilder builder = new URIBuilder(url);
    builder.setParameter("code", genGoogleCode());
    HttpGet httpGet = new HttpGet(builder.build());
    httpGet.setConfig(config);
    response = this.httpClient.execute(httpGet);
    } catch (Exception e) {
    LOGGER.error("文件下载失败 {}", e);
    throw new MultipartException("文件下载失败");
    }
    try (InputStream inputStream = response.getEntity().getContent()) {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
    return byteArrayInputStream;
    } catch (Exception e) {
    LOGGER.error("文件服务器获取Outputstream 错误!{}", e);
    throw new MultipartException("文件服务器获取Output Stream 错误");
    }
    }
    }
  • 相关阅读:
    MPlayer源代码分析
    洛谷 P1025 数的划分
    洛谷 P1209 [USACO1.3]修理牛棚 Barn Repair
    洛谷 P1744 采购特价商品
    洛谷 P1443 马的遍历
    洛谷 P1294 高手去散步
    洛谷 P2104 二进制
    洛谷 P1088 火星人
    洛谷 P1062 数列
    洛谷 P2005 A/B Problem II
  • 原文地址:https://www.cnblogs.com/yhc-love-cl/p/13596256.html
Copyright © 2011-2022 走看看