httppost调用远程服务器的jar-创新互联
场景:思路我需要实现的是solr配置文件的自动生成(即本地给了文件和文件名,服务器要获取到文件并按规则生成)
大名网站建设公司创新互联,大名网站设计制作,有大型网站制作公司丰富经验。已为大名成百上千提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的大名做网站的公司定做!
代码 服务器上的jar(生成文件代码)文件生成是考虑直接部署在服务器上,所以写一个java程序用来接受文件,开始创建文件
本地使用http的方式去调用服务器上的java程序,并写入文件
public static void makeConfig(MultipartFile[] files, String coreName) throws IOException {File coreHomeFile = new File("/data/engine/solr-7.7.3/server/solr/" + coreName);
File coreConfFile = new File(coreHomeFile + "/conf");
File coreLangFile = new File(coreConfFile + "/lang");
//构建
if (!(coreLangFile.exists())) {System.out.println("文件不存在开始创建文件夹");
coreLangFile.mkdirs();
}
for (MultipartFile file : files) {if (Objects.equals(file.getOriginalFilename(), "managed-schema") || Objects.equals(file.getOriginalFilename(), "params.json") || Objects.equals(file.getOriginalFilename(), "protwords.txt") || Objects.equals(file.getOriginalFilename(), "solrconfig.xml") || Objects.equals(file.getOriginalFilename(), "stopwords.txt") || Objects.equals(file.getOriginalFilename(), "synonyms.txt")) {InputStream inputStream = file.getInputStream();
System.out.println("11:"+coreConfFile + "/" + file.getOriginalFilename());
BufferedOutputStream outputStream = FileUtil.getOutputStream(coreConfFile + "/" + file.getOriginalFilename());
IoUtil.copy(inputStream,outputStream,IoUtil.DEFAULT_BUFFER_SIZE);
}else {System.out.println("开始下载:"+file.getOriginalFilename());
InputStream inputStream = file.getInputStream();
System.out.println("11:"+coreLangFile + "/" + file.getOriginalFilename());
BufferedOutputStream outputStream = FileUtil.getOutputStream(coreLangFile + "/" + file.getOriginalFilename());
IoUtil.copy(inputStream,outputStream,IoUtil.DEFAULT_BUFFER_SIZE);
}
}
System.out.println("下载完成");
}
@PostMapping("/make")
public void make(@RequestParam("files") MultipartFile[] files, String coreName) throws IOException {FileUtils.makeConfig(files, coreName);
}
本地代码(访问服务器jar的代码)@PostMapping("/io")
public void io(@RequestParam MultipartFile[] files, String coreName) throws IOException {HttpUtils.testGenMakeConfig(files,coreName);
}
public static void testGenMakeConfig(MultipartFile[] files,String coreName) throws IOException {CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// HttpPost httpPost = new HttpPost("http://localhost:8081/make");
HttpPost httpPost = new HttpPost("http://192.168.*.*:9998/make");
CloseableHttpResponse response = null;
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
for (MultipartFile file : files) {System.out.println("fileName:"+file.getOriginalFilename());
multipartEntityBuilder.addBinaryBody("files",file.getInputStream(),ContentType.DEFAULT_BINARY,file.getOriginalFilename());
}
ContentType contentType = ContentType.create("text/plain", StandardCharsets.UTF_8);
multipartEntityBuilder.addTextBody("coreName", "test-demo", contentType);
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println("HTTPS响应状态为:" + response.getStatusLine());
if (responseEntity != null) {System.out.println("HTTPS响应内容长度为:" + responseEntity.getContentLength());
// 主动设置编码,来防止响应乱码
String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
System.out.println("HTTPS响应内容为:" + responseStr);
}
}
PS:
multipartEntityBuilder.addBinaryBody(“files”,file.getInputStream(),ContentType.DEFAULT_BINARY,file.getOriginalFilename());
这个方法的后两个参数一定要写,虽然不写不会报错,但是文件在服务器上会接收不到
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前文章:httppost调用远程服务器的jar-创新互联
URL网址:http://azwzsj.com/article/dsiogd.html