java 实现post文件提交

1、引入相关的jar包

  
      org.apache.httpcomponents
      httpmime
      4.5.3
  

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文件提交)