此代码从JAR包读取固件升级文件,并作为Post请求发送到设备:
CloseableHttpClient client2 = HttpClients.createDefault();
HttpPost post = new HttpPost("http://" + IP + "/moxa-cgi/UploadFirmwareFile.cgi");
InputStream stream = Main.class.getResourceAsStream("vport364a_v1_5.rom");
byte b[] = new byte[stream.available()];
stream.read(b);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("uploadfile", b, ContentType.APPLICATION_OCTET_STREAM, "vport364a_v1_5.rom");
post.setEntity(builder.build());
HttpResponse response2 = client2.execute(post);
由于未知原因,代码停止作为执行jar包工作:固件升级文件作为空值发送。在线:
stream.read(b);
只有空值。 如果我在IDE Netbeans中运行代码,它就可以工作。为什么?