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

public class post {

// 如果是文本的文件的话那么通过map类传递进来如果是文件的话通过FormFile传递进来
public static String post(String actionUrl, Map<String, String> 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<String, String> 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根据 uri 获得文件真实路径
  2. 清单文件,测试,打电话和发短信应用
  3. Android中解析xml文件的接口
  4. Android 各种类型的资源 文件

随机推荐

  1. Unity Android(安卓)在Windows环境下使用
  2. android 控制POS机图文打印(一)
  3. Android(安卓)NDK开发入门实例
  4. Flutter for Android开发详细配置开创建
  5. ELF文件查看利器之readelf用法
  6. 数据共享的方法
  7. lame音频压缩解码(二)之编译事例Demo
  8. android 仿淘宝实现标题栏滑动渐变效果,gi
  9. Android(安卓)Ndk: 如何从native层直接获
  10. 浅谈android view事件分发机制