httpclient通过附件形式传递,客户端和服务端实现方法

1、客户端发送实现

 

package url;

import io.IoStreamUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * httpclient上传文件
 * @author linwei
 *
 */
public class MultipartEntityUtil {

 public static String postFile(File file,String url) throws ClientProtocolException, IOException {

  FileBody bin = null;
  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost(url);
  if(file != null) {
   bin = new FileBody(file);
  }

  StringBody username = new StringBody("13696900475");
  StringBody password = new StringBody("13696900475");
//请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好
                //new StringBody("汉字",Charset.forName("UTF-8"))); 
  
     MultipartEntity reqEntity = new MultipartEntity();
     reqEntity.addPart("username", username);
     reqEntity.addPart("password", password);
     reqEntity.addPart("data", bin);
    
     httppost.setEntity(reqEntity);
     System.out.println("执行: " + httppost.getRequestLine());
    
     HttpResponse response = httpclient.execute(httppost);
     System.out.println("statusCode is " + response.getStatusLine().getStatusCode());
     HttpEntity resEntity = response.getEntity();
     System.out.println("----------------------------------------");
     System.out.println(response.getStatusLine());
     if (resEntity != null) {
       System.out.println("返回长度: " + resEntity.getContentLength());
       System.out.println("返回类型: " + resEntity.getContentType());
       InputStream in = resEntity.getContent();
       System.out.println("in is " + in);
       System.out.println(IoStreamUtil.getStringFromInputStream(in));
     }
     if (resEntity != null) {
       resEntity.consumeContent();
     }
  return null;
 }
 
 public static void main(String[] args) throws ClientProtocolException, IOException {
 
  File file = new File("d:/rss.xml");
  String url = "http://localhost:8080/webtest/servlet/URLTest";
  postFile(file,url);
 }
 
}

 

2、服务端接收实现

 

//commons-fileupload.jar  commons-io
request.setCharacterEncoding("UTF-8");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
          FileItemFactory factory = new DiskFileItemFactory(); 
            ServletFileUpload upload = new ServletFileUpload(factory); 
            try {  
                List items = upload.parseRequest(request); 
                Iterator iter = items.iterator(); 
                while (iter.hasNext()) { 
                    FileItem item = (FileItem) iter.next(); 
                    if (item.isFormField()) { 
                        //普通文本信息处理 
                        String paramName = item.getFieldName(); 
                        String paramValue = item.getString(); 
                        System.out.println(paramName + ":" + paramValue); 
                    } else { 
                        //上传文件信息处理 
                        String fileName = item.getName(); 
                        byte[] data = item.get(); 
                        String filePath = "d:/ssssss.txt"; 
                        FileOutputStream fos = new FileOutputStream(filePath); 
                        fos.write(data); 
                        fos.close(); 
                    } 
                }  
            } catch (FileUploadException e) { 
                e.printStackTrace(); 
            } 
        } 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值