阅读更多 android 文件上传的类--完整 可以直接被调用的

public class post {

// 如果是文本的文件的话那么通过map类传递进来如果是文件的话通过FormFile传递进来
public static String post(String actionUrl, Map params,
   FormFile[] files) throws IOException {

  String BOUNDARY = "743520vjdk4e";
  String MULTIPART_FROM_DATA = "multipart/form-data";

  URL uri = new URL(actionUrl);
  HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
  conn.setReadTimeout(5 * 1000); // 缓存的最长时间
  conn.setDoInput(true);// 允许输入
  conn.setDoOutput(true);// 允许输出
  conn.setUseCaches(false); // 不允许使用缓存
  // 下面的几个值是必须需要设置进去的
  conn.setRequestMethod("POST");
  conn.setRequestProperty("connection", "keep-alive");
  conn.setRequestProperty("Charsert", "UTF-8");
  conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
    + ":boundary" + BOUNDARY);

  // 首先组拼文本类型的参数
  StringBuilder sb = new StringBuilder();
 
  // 这个地方使用了Map循环 map循环的方式需要注意一下了
  for (Map.Entry entry : params.entrySet()) {
   sb.append("--");
   sb.append(BOUNDARY);
   // 回车换行
   sb.append("\r\n");
   sb.append("Content-Disposition:form-data:name\"" + entry.getKey()
     + "\r\n\r\n");
   sb.append(entry.getValue());
   sb.append("\r\n");
  }
  DataOutputStream outStream = new DataOutputStream(conn
    .getOutputStream());
  outStream.write(sb.toString().getBytes());

 

  // 前面必须是数组才可以
  // 发送文件数据
  for (FormFile file : files) {

   StringBuilder sb1 = new StringBuilder();
   sb1.append("---");
   sb1.append(BOUNDARY);
   sb1.append("\r\n");
   // 这个地方没有完
   sb1.append("Content-Disposition:form-data:name=\""
     + file.getFormname());
   sb1.append("Content-Type" + file.getContentType() + "\r\n\r\n");
   outStream.write(sb1.toString().getBytes());

   // 先判断formfile里面是否为空 如果不为空的话则写出 获取formfile的data里面的
   if (file.getInStream() != null) {
    // 提供流的的方式的话就是一边读一边写了
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = file.getInStream().read(buffer)) != -1) {
     outStream.write(buffer, 0, len);
    }
    file.getInStream().close();
   } else {
    outStream.write(file.getData(), 0, file.getData().length);

   }
   outStream.write("\r\n".getBytes());

  }
  byte[] end_data = ("--" + BOUNDARY + "\r\n").getBytes();
  outStream.write(end_data);
  outStream.flush();

  // 得到响应号码
  int res = conn.getResponseCode();
  if (res != 200)
   throw new RuntimeException("请求失败 ");
  InputStream in = conn.getInputStream();
  int ch;
  StringBuilder sb2 = new StringBuilder();
  while ((ch = in.read()) != -1) {
   sb.append((char) ch);
  }
  outStream.close();
  conn.disconnect();
  return in.toString();
}









这是相关联的formFIle类的定义


public class FormFile {

// 定义了使用的文件的特点
// 上传文件的数据
private byte[] data;
private InputStream inStream;
// 文件名称
private String fileName;
// 请求参数名称
private String Formnames;
// 内容类型
private String contentType = "application/octet-stream";
public FormFile(byte[] data, String fileName, String formnames,
   String contentType) {
  this.data = data;
  this.fileName = fileName;
  Formnames = formnames;
  this.contentType = contentType;
}
public FormFile(InputStream inStream, String fileName, String formnames,
   String contentType) {
  this.inStream = inStream;
  this.fileName = fileName;
  Formnames = formnames;
  this.contentType = contentType;
}
public byte[] getData() {
  return data;
}
public void setData(byte[] data) {
  this.data = data;
}
public InputStream getInStream() {
  return inStream;
}
public void setInStream(InputStream inStream) {
  this.inStream = inStream;
}
public String getFileName() {
  return fileName;
}
public void setFileName(String fileName) {
  this.fileName = fileName;
}
public String getFormnames() {
  return Formnames;
}
public void setFormnames(String formnames) {
  Formnames = formnames;
}
public String getContentType() {
  return contentType;
}
public void setContentType(String contentType) {
  this.contentType = contentType;
}

更多相关文章

  1. Android缺少awk:安装busybox
  2. Android(安卓)FileObserver 用法
  3. NDK/JNI02-实例开发流程
  4. 更改Android模拟器中的hosts文件
  5. android学习笔记(4)-android的文件的操作模式与单元测试
  6. Android(安卓)Studio修改默认Activity继承AppCompatActivity
  7. Android(安卓)APK签名
  8. 更好用的Android(安卓)Toast
  9. NPM 和webpack 的基础使用

随机推荐

  1. MySQL -如何在最小/最大日期差异超过3年
  2. Yahoo,Msn,Skype,QQ,阿里旺旺在线聊天链
  3. 如果条件按顺序,Mysql不能使用mysql
  4. 利用TPC-H为MYSQL生成数据
  5. 如何最好地处理重复日历事件的异常
  6. mysql中逗号分隔字段的更好替代方案
  7. Mysql--可用的 MySQL 产品和专业服务
  8. mysql5.6.23安装 步骤
  9. MySQL2 Ruby gem不会安装10.6
  10. Invalid property 'driver_class' of bea