1. org.apache.commons.httpclient.HttpClient
1.1 pom
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency>
1.2 code
package com.skd.util; import com.skd.common.Constant; import com.skd.common.Event; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @Description * @Author virgosnail * @Date 2018/12/15 17:06 */ public class HttpUtil2 { private static Logger log = LoggerFactory.getLogger(HttpUtil2.class); /** * org.apache.commons.httpclient.HttpClient */ private static HttpClient httpClient = new HttpClient(); /** * @param event 0:刪除 1:新增 2:修改 * @param file * @return * @Author Administrator * @Date 2018年11月11日 */ public static void doPostMethod(Event event, File file) { try { PostMethod post = new PostMethod(FileUtil.getURL()); post.setRequestHeader("Content-Type","application/json"); List<Part> list = new ArrayList<>(); list.add(new StringPart(Constant.TYPE, event.getValue())); list.add(new StringPart(Constant.RELATIVE_PATH, file.getPath())); if (!Event.FILE_DELETE.equals(event)) {
// list.add(new FilePart(Constant.FILE, file)); } Part[] parts = new Part[list.size()]; list.toArray(); post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); log.info("request: " + post); httpClient.executeMethod(post); String response = post.getResponseBodyAsString(); log.info("respopnse: " + response); } catch (Exception e) { log.error("doPostMethod occur a exception", e); } } }
2. org.apache.http.client.HttpClient
2.1 pom
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.3</version> </dependency>
2.2 code
package com.skd.util; import com.skd.common.Constant; import com.skd.common.Event; import net.sf.json.JSONObject; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.nio.charset.Charset; /** * @Description * @Author virgosnail * @Date 2018/12/15 17:06 */ public class HttpUtil { private static Logger log = LoggerFactory.getLogger(HttpUtil.class); /** * org.apache.http.client.HttpClient; * 創建一個httpclient對象 */ private static HttpClient httpclient = HttpClients.createDefault(); /** * @param event 0:刪除 1:新增 2:修改 * @param file * @return * @Author Administrator * @Date 2018年11月11日 */ public static void doPost(Event event, File file, boolean isDir, boolean hasFile) { try { // 創建http的發送方式對象,是GET還是POST String url = FileUtil.getURL(); HttpPost httppost = new HttpPost(url); log.info(" URL: " + url); httppost.addHeader("Charset",Constant.CHARSET); // 創建要發送的實體,就是key-value的這種結構,借助於這個類,可以實現文件和參數同時上傳 MultipartEntityBuilder fileEntity = MultipartEntityBuilder.create(); // 設置編碼 Charset charset = Charset.forName(Constant.CHARSET); fileEntity.setCharset(charset); // 追加要發送的文本信息並設置編碼格式 fileEntity.addTextBody(Constant.TYPE, event.getValue(),ContentType.APPLICATION_JSON); log.info("type: " + event.getValue()); String relativePath = FileUtil.getRelativePath(file.getAbsolutePath()); fileEntity.addTextBody(Constant.RELATIVE_PATH, relativePath,ContentType.APPLICATION_JSON); log.info("relative_path: " + relativePath); fileEntity.addTextBody(Constant.ISDIR, String.valueOf(isDir),ContentType.APPLICATION_JSON); log.info("isDir: " + isDir); if (Event.FILE_CREATE.equals(event) || Event.FILE_CHANGE.equals(event)){ fileEntity.addPart(Constant.FILE, new FileBody(file,ContentType.APPLICATION_OCTET_STREAM)); } log.info("file: " + file); HttpEntity httpEntity = fileEntity.build(); httppost.setEntity(httpEntity); // 執行httppost對象並獲得response HttpResponse response = httpclient.execute(httppost); // 狀態碼 int statusCode = response.getStatusLine().getStatusCode(); HttpEntity resEntity = response.getEntity(); // 獲得返回來的信息,轉化為字符串string String respopnse = EntityUtils.toString(resEntity); log.info("respopnse body: " + respopnse); } catch (Exception e) { log.error("doPost occur a exception", e); } } }