| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package com.happy.common.util;
- import java.io.File;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.mime.MultipartEntityBuilder;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.entity.mime.content.StringBody;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- /**
- * 调用跨网文件发送接口
- * @author tingh
- *
- */
- public class HttpSendFile {
- private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");
- /**
- * 调用文件发送接口
- * @param file
- * @param
- * @param remote_url
- * @return
- */
- public static Map<String, Object> call_sendFile(File file,String FileName,String remote_url){
- CloseableHttpClient httpclient = HttpClients.createDefault();
- Map<String, Object> responseMap = new HashMap<>();
- String result = "";
- try{
- HttpPost httpPost = new HttpPost(remote_url);
- //创建接口需要的参数
- MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
- entityBuilder.addPart("myFile", new FileBody(file));
- entityBuilder.addPart("myFileFileName", new StringBody(FileName));
- HttpEntity entity = entityBuilder.build();
- httpPost.setEntity(entity);
- //调用跨网文件发送接口
- HttpResponse response = httpclient.execute(httpPost);
- //获取响应信息
- HttpEntity responseEntity = response.getEntity();
- if(responseEntity != null){
- result = EntityUtils.toString(responseEntity, CHARSET_UTF8);
- System.out.println(result);
- }
- responseMap.put("code", response.getStatusLine().getStatusCode());
- responseMap.put("result", result);
- } catch (IOException e){
- e.printStackTrace();
- } catch (Exception e){
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- return responseMap;
- }
- public static void main(String[] args) {
- File file = new File("F://1.jpg");
- call_sendFile(file,"1111.jpg","http://localhost:8088/fileload/loadhimage.action");
- }
- }
|