HttpResponse response=null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
String url="http://192.168.1.135:3000/v2/accounts/jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh/upload?validated=true";
File file=new File("D:","v2.0.2.pdf");
//输入流,
InputStream in = new FileInputStream(file);
HttpPost httpPost = new HttpPost(url);
/**设置 contenttype 以form形式上传文件, 强制使用UTF-8编码**/
ContentType type=ContentType.MULTIPART_FORM_DATA.withCharset("UTF-8");
HttpEntity httpEntity = MultipartE
ntityBuilder
.create()
.addPart("secret",new StringBody("snoPBjXtMeMyMHUVTgbuqAfg1SUTb",type))
.addPart("DatagramSN",new StringBody(new Date().getTime()+"嗷嗷嗷啊",type))
.addPart("destination",new StringBody("jJpba91JvNw9PCmCfxUFjJyBGdrro9ThnA",type))
.addPart("amount", new StringBody("1",type))
.addPart("DatagramType",new StringBody(type.getMimeType(),type))
.addPart("TerminalID",new StringBody("呵呵",type))
.addPart("NodeID", new StringBody("你好",type))
/**
* 以原生文件形式 上传文件
*/
// .addPart("DatagramName", new FileBody(file))
/**
* 以流的形式上传 并且使用base64 对文件名称 进行加密
*/
// .addBinaryBody("DatagramName", in,type,Base64.getEncoder().encodeToString("银杏树标准服务接口v2.0.2.pdf".getBytes()))
/**
* 以流的形式上传
*/
// .addBinaryBody("DatagramName", in,type,file.getName())
/**
* 以文件形式上传 并且使用base64 对文件名称 进行加密
*/
// .addBinaryBody("DatagramName", new File("D:\\timg.jpg"),type,Base64.getEncoder().encodeToString("timg.jpg".getBytes()))
/**
*文件上传 添加 contenttype
*/
.addBinaryBody("DatagramName", file,type,file.getName())
/**
* 强制使用UTF-8
*/
.setCharset(Charset.forName("UTF-8"))
/**
* 添加浏览器兼容性
*/
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.build();
httpPost.setEntity(httpEntity);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000)
.setConnectTimeout(10000)
.build();
httpPost.setConfig(requestConfig);
response = httpclient.execute(httpPost);
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
String url="http://192.168.1.135:3000/v2/accounts/jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh/upload?validated=true";
File file=new File("D:","v2.0.2.pdf");
//输入流,
InputStream in = new FileInputStream(file);
HttpPost httpPost = new HttpPost(url);
/**设置 contenttype 以form形式上传文件, 强制使用UTF-8编码**/
ContentType type=ContentType.MULTIPART_FORM_DATA.withCharset("UTF-8");
HttpEntity httpEntity = MultipartE
ntityBuilder
.create()
.addPart("secret",new StringBody("snoPBjXtMeMyMHUVTgbuqAfg1SUTb",type))
.addPart("DatagramSN",new StringBody(new Date().getTime()+"嗷嗷嗷啊",type))
.addPart("destination",new StringBody("jJpba91JvNw9PCmCfxUFjJyBGdrro9ThnA",type))
.addPart("amount", new StringBody("1",type))
.addPart("DatagramType",new StringBody(type.getMimeType(),type))
.addPart("TerminalID",new StringBody("呵呵",type))
.addPart("NodeID", new StringBody("你好",type))
/**
* 以原生文件形式 上传文件
*/
// .addPart("DatagramName", new FileBody(file))
/**
* 以流的形式上传 并且使用base64 对文件名称 进行加密
*/
// .addBinaryBody("DatagramName", in,type,Base64.getEncoder().encodeToString("银杏树标准服务接口v2.0.2.pdf".getBytes()))
/**
* 以流的形式上传
*/
// .addBinaryBody("DatagramName", in,type,file.getName())
/**
* 以文件形式上传 并且使用base64 对文件名称 进行加密
*/
// .addBinaryBody("DatagramName", new File("D:\\timg.jpg"),type,Base64.getEncoder().encodeToString("timg.jpg".getBytes()))
/**
*文件上传 添加 contenttype
*/
.addBinaryBody("DatagramName", file,type,file.getName())
/**
* 强制使用UTF-8
*/
.setCharset(Charset.forName("UTF-8"))
/**
* 添加浏览器兼容性
*/
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.build();
httpPost.setEntity(httpEntity);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000)
.setConnectTimeout(10000)
.build();
httpPost.setConfig(requestConfig);
response = httpclient.execute(httpPost);