美文网首页
java 实现post文件提交

java 实现post文件提交

作者: 周六不算加班 | 来源:发表于2018-11-29 17:38 被阅读27次

1、引入相关的jar包

  <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.5.3</version>
  </dependency>

2、实现类

public void moveWaterMark(File file) {
    try {
        if (file.exists()) {
            HttpClient client = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost("http://192.168.3.106:8888/upload");
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("file", new FileBody(file));

            httpPost.setEntity(entity);
            HttpResponse response = client.execute(httpPost);

            HttpEntity responseEntity = response.getEntity();
            String result = EntityUtils.toString(responseEntity);
            System.out.println(result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

相关文章

网友评论

      本文标题:java 实现post文件提交

      本文链接:https://www.haomeiwen.com/subject/xfgfcqtx.html