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 錯誤");
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM