如何使用java将文件发布到URL
public static void main(String[] args) {
String addURL = "https://xxxxx.service-now.com/sys_ms_project_import.do";
String user = "xxx";
String pwd = "xxxx";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost postMethod = new HttpPost (addURL);
try {
postMethod.addHeader("Authorization", "BASIC" + " " +
new String(org.apache.commons.codec.binary.Base64.encodeBase64(new String(user + ":" + pwd).getBytes("UTF-8"))));
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
String fileName = "D:\\xxxx\\xxx\\xxxxx.mpp";
File file = new File(fileName);
builder.addBinaryBody(
"file",
new FileInputStream(file),
ContentType.APPLICATION_XML,
file.getName()
);
HttpEntity multipart = builder.build();
postMethod.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(postMethod);
System.out.println(response.getStatusLine());
HttpEntity responseEntity = response.getEntity();
}catch(Exception e){
e.printStackTrace();
}
}
我现在有一个正在运行的面向internet的服务实例。它有一个将mpp文件导入其中的UI。UI很简单,只是有一个表单标签和与之关联的操作,还有一个文件浏览控件和提交按钮。从UI导入时,现在已成功将文件导入服务。我不知道它的内部功能
现在我的要求是,我想从本地机器或者说从不同机器上的java类实现相同的功能
我知道我可以使用ApacheHttpClient来做到这一点。但我不确定这是否能做到。我可以这样做吗?这样就可以实现与通过UI提交的相同的功能
Here is the UI to import file in service now
下面是上面UI的HTML代码
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<form action="sys_ms_project_import.do" method="post" enctype="multipart/form-data" onsubmit="return validateForm()">
<input size="41" id="importFile" name="importFile" type="file"></input>
<input value="Import" style="width: 85px;" width="85" type="submit"></input>
</form>
</j:jelly>
谢谢, 苏尼尔
共 (0) 个答案