码迷,mamicode.com
首页 > Web开发 > 详细

使用httpclient传输文件

时间:2015-10-27 20:23:00      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

客户端代码:

package httpclient;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
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;

public class HttpClientTest {
 public static void main(String[] args) throws Exception {
  HttpClientTest httpClientTest = new HttpClientTest();
  httpClientTest.httpFileTest();
 }

 public void httpFileTest() throws Exception {
  File localFile = new File("D:\\httptest.txt");
  File localFile1 = new File("D:\\httptest1.txt");
  String url = "http://10.136.2.10:8080/yld-base-webapp-modules-appconnectorin/api/test.do";
  PostMethod filePost = new PostMethod(url);
  FilePart fp = new FilePart("file", localFile);
  FilePart fp1 = new FilePart("file1", localFile1);
  // StringPart:普通的文本参数
  StringPart uname = new StringPart("username", "aa");
  StringPart pass = new StringPart("password", "123456");
  Part[] parts = { uname, pass, fp,fp1 };
  // 对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
  MultipartRequestEntity mre = new MultipartRequestEntity(parts, filePost.getParams());
  filePost.setRequestEntity(mre);
  HttpClient client = new HttpClient();
  int status = client.executeMethod(filePost);
  System.out.println(status + "--------------");

 }
}

服务端代码:

package com.yld.publicservice.springweb.service;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.boco.common.ReturnValue;

/**
 * 解析httpclient中上传的文件,只能处理文件--如果要记录其他信息(如表单域中的其他信息),参考监控工程中的方法
 * 实现原理:解析请求数据流
 * @author  boco
 */
@Service
public class FileUpLoadService {
 private List<String> fileRealNames = new ArrayList<String>();
 private static final String outFilePath = "/home/lilong/uptoserver/";

 public List<String> getFileRealNames() {
  return fileRealNames;
 }

 /**
  * 提供给外部调用的接口
  *
  * @param  request
  * @return
  */
 public ReturnValue fileUpLoad(HttpServletRequest request) {
  ReturnValue returnValue = fileUpLoadMain(request);
  return returnValue;
 }

 /**
  * 文件上传的主服务
  *
  * @param  request
  * @return
  */
 @SuppressWarnings("unused")
 private ReturnValue fileUpLoadMain(HttpServletRequest request) {
  final int NONE = 0; // 状态码,表示没有特殊操作
  final int DATAHEADER = 1; // 表示下一行要读到报头信息
  // 请求消息实体的总长度(请求消息中除消息头之外的数据长度)
  int totalbytes = request.getContentLength();
  File file; // 上传文件储存在服务器上
  // 容纳请求消息实体的字节数组
  byte[] dataOrigin = new byte[totalbytes];
  // 对于post多个文件的表单,b作为原始数据的副本提供提取文件数据的操作
  byte[] b = new byte[totalbytes];
  // 请求消息类型
  String contentType = request.getContentType();
  String fieldname = ""; // 表单域的名称
  String fieldvalue = ""; // 表单域的值
  String fileFormName = ""; // 上传的文件再表单中的名称
  String fileRealName = ""; // 上传文件的真实名字
  String boundary = ""; // 分界符字符串
  String lastboundary = ""; // 结束分界符字符串
  int fileSize = 0; // 文件长度
  // 容纳表单域的名称/值的哈希表
  Map<String, String> formfieldsTable = new HashMap<String, String>();
  // 容纳文件域的名称/文件名的哈希表
  Map<String, String> filenameTable = new HashMap<String, String>();
  // 在消息头类型中找到分界符的定义
  int pos = contentType.indexOf("boundary=");
  int pos2;
  try {
   if (pos != -1) {
    pos += "boundary=".length();
    boundary = "--" + contentType.substring(pos); // 解析出分界符
    lastboundary = boundary + "--"; // 得到结束分界符
   }
   int state = NONE; // 起始状态为NONE
   // 得到请求消息的数据输入流
   DataInputStream in = new DataInputStream(request.getInputStream());
   in.readFully(dataOrigin); // 根据长度,将消息实体的内容读入字节数组dataOrigin中
   in.close(); // 关闭数据流
   String reqcontent = new String(dataOrigin); // 从字节数组中得到表示实体的字符串
   // 从字符串中得到输出缓冲流
   BufferedReader reqbuf = new BufferedReader(new StringReader(
     reqcontent));
   // 设置循环标志
   boolean flag = true;
   while (flag == true) {
    String s = reqbuf.readLine();
    if (s == lastboundary || s == null)
     break;
    switch (state) {
    case NONE:
     if (s.startsWith(boundary)) {
      // 如果读到分界符,则表示下一行一个头信息
      state = DATAHEADER;
     }
     break;
    case DATAHEADER:
     pos = s.indexOf("filename=");
     // 先判断出这是一个文本表单域的头信息,还是一个上传文件的头信息
     if (pos == -1) {
      // 如果是文本表单域的头信息,解析出表单域的名称---这个方法中什么都没有做
      state = NONE; // 设置状态码,准备读取表单域的值
     } else {
      // 如果是文件数据的头,先存储这一行,用于在字节数组中定位
      // 这个方法是将文件数据直接进行存盘,下面读取文件数据的基本上是没多大用处的
      String temp = s;
      // 先解析出文件名
      pos = s.indexOf("name=");
      pos += "name=".length() + 1; // 1表示后面的"的占位
      pos2 = s.indexOf("filename=");
      String s1 = s.substring(pos, pos2 - 3); // 3表示";加上一个空格,解析出来的是name的值
      fileFormName = s1;
      pos2 += "filename=".length() + 1; // 1表示后面的"的占位
      s = s.substring(pos2);
      int l = s.length();
      s = s.substring(0, l - 1);// 解析出来的是filename的值
      pos2 = s.lastIndexOf("\\");// 如果filename中有/符号,需要进行的操作
      s = s.substring(pos2 + 1);
      fileRealName = s;
      System.out.println("fileRealName:" + fileRealName);
      if (fileRealName.length() != 0) { // 确定有文件被上传
       s = reqbuf.readLine();
       boolean isContent = true;
       //循环数据-将请求头的数据过滤掉
       while(isContent){
        if(s.indexOf("Content") != -1){
         s = reqbuf.readLine();
        }else{
         isContent = false;
        }
       }
       //过滤掉请求头下面的空行---每个请求都有的
       s = reqbuf.readLine();
       //上传文件输出的地址文件
       file = new File(outFilePath + File.separator
         + fileRealName);
       DataOutputStream fileout = new DataOutputStream(
         new FileOutputStream(file));
       //循环写入到输出文件中
       while ((!s.startsWith(boundary))
         && (!s.startsWith(lastboundary))) {
        fileout.writeChars(s);
        fileout.writeChars("\r\n");
        s = reqbuf.readLine();
        //如果读到分解符开始下一次的判断
        if (s.startsWith(boundary)) {
         state = DATAHEADER;
         break;
        }
       }
      } 
     } 
    //结束switch
    break;
    }
   }
   return ReturnValue.getInstance(fileRealName, 0);
  } catch (Exception e) {
   e.printStackTrace();
   return ReturnValue.getInstance(1);
  }
 }

 /**
  * 在原始数据(b)中找到s数据在b中的位置数据
  *
  * @param b
  *            :原始数据
  * @param s
  *            :需要操作的数据
  * @param start
  *            :起始位置
  * @return
  */
 private static int byteIndexOf(byte[] b, String s, int start) {
  return byteIndexOf(b, s.getBytes(), start);
 }

 private static int byteIndexOf(byte[] b, byte[] s, int start) {
  int i;
  if (s.length == 0) {
   return 0;
  }
  int max = b.length - s.length;
  if (max < 0) {
   return -1;
  }
  if (start > max) {
   return -1;
  }
  if (start < 0) {
   start = 0;
  }
  // 在b中找到s的第一个元素
  search: for (i = start; i <= max; i++) {
   if (b[i] == s[0]) {
    // 找到了s中的第一个元素后,比较剩余的部分是否相等
    int k = 1;
    while (k < s.length) {
     if (b[k + i] != s[k]) {
      continue search;
     }
     k++;
    }
    return i;
   }
  }
  return -1;
 }

 /**
  * 赋值数组
  *
  * @param b
  *            :源数组
  * @param from
  *            :起始位置
  * @param end
  *            :结束位置
  * @return
  */
 private static byte[] subBytes(byte[] b, int from, int end) {
  byte[] result = new byte[end - from];
  System.arraycopy(b, from, result, 0, end - from);
  return result;
 }

}

 

使用httpclient传输文件

标签:

原文地址:http://my.oschina.net/u/2433822/blog/522785

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!