public void proxyUploadFile() throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpHost target = new HttpHost("192.168.71.128", 8080); //目的服務器地址 HttpHost proxy = new HttpHost("172.16.44.22", 9901, "http"); //代理服務器地址 RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpPost request = new HttpPost("/upload/file/"); //目的服務器上傳文件接口 request.setConfig(config); //add file File file = new File("/home/mydoc.docx"); HttpEntity data = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addPart("file", new FileBody(file)).build(); request.setEntity(data); getLogger().info("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); try { getLogger().info("----------------------------------------"); getLogger().info(response.getStatusLine()); getLogger().info(EntityUtils.toString(response.getEntity())); //調用上傳文件接口返回結果 } finally { IOUtils.closeQuietly(response); } } finally { IOUtils.closeQuietly(httpclient); } return; }
以httpClient通過代理上傳文件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.