- 利用 apache httpmime.jar包
- maven配置如下
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpmime</artifactId>
- <version>4.0.2</version>
- </dependency>
- public static void main(String[] args) throws ClientProtocolException,
- IOException {
- HttpClient httpclient = new DefaultHttpClient();
- //请求处理页面
- HttpPost httppost = new HttpPost(
- "http://localhost:8080/webtools/upload.jsp");
- //创建待处理的文件
- FileBody file = new FileBody(new File("d:/22.rar"));
- //对请求的表单域进行填充
- MultipartEntity reqEntity = new MultipartEntity();
- reqEntity.addPart("fileField", file);
- //设置请求
- httppost.setEntity(reqEntity);
- //执行
- HttpResponse response = httpclient.execute(httppost);
- if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
- HttpEntity entity = response.getEntity();
- }
- }
相关文章: