当前位置: 移动技术网 > IT编程>移动开发>Android > Android通过HTTP协议实现上传文件数据

Android通过HTTP协议实现上传文件数据

2019年07月24日  | 移动技术网IT编程  | 我要评论

怪探德克,黄家驹没死,梁冠华老婆

本文实例为大家分享了android通过http协议实现上传文件数据的具体代码,供大家参考,具体内容如下

sockethttprequester.java

package cn.itcast.utils;

import java.io.bufferedreader;
import java.io.bytearrayoutputstream;
import java.io.dataoutputstream;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.net.httpurlconnection;
import java.net.inetaddress;
import java.net.socket;
import java.net.url;
import java.net.urlencoder;
import java.util.arraylist;
import java.util.list;
import java.util.map;

import org.apache.http.httpresponse;
import org.apache.http.namevaluepair;
import org.apache.http.client.httpclient;
import org.apache.http.client.entity.urlencodedformentity;
import org.apache.http.client.methods.httppost;
import org.apache.http.impl.client.defaulthttpclient;
import org.apache.http.message.basicnamevaluepair;

public class sockethttprequester {
 /**
 * 发送xml数据
 * @param path 请求地址
 * @param xml xml数据
 * @param encoding 编码
 * @return
 * @throws exception
 */
 public static byte[] postxml(string path, string xml, string encoding) throws exception{
 byte[] data = xml.getbytes(encoding);
 url url = new url(path);
 httpurlconnection conn = (httpurlconnection)url.openconnection();
 conn.setrequestmethod("post");
 conn.setdooutput(true);
 conn.setrequestproperty("content-type", "text/xml; charset="+ encoding);
 conn.setrequestproperty("content-length", string.valueof(data.length));
 conn.setconnecttimeout(5 * 1000);
 outputstream outstream = conn.getoutputstream();
 outstream.write(data);
 outstream.flush();
 outstream.close();
 if(conn.getresponsecode()==200){
  return readstream(conn.getinputstream());
 }
 return null;
 }
 
 /**
 * 直接通过http协议提交数据到服务器,实现如下面表单提交功能:
 *  <form method=post action="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data">
  <input type="text" name="name">
  <input type="text" name="id">
  <input type="file" name="imagefile"/>
   <input type="file" name="zip"/>
  </form>
 * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)
 * @param params 请求参数 key为参数名,value为参数值
 * @param file 上传文件
 */
 public static boolean post(string path, map<string, string> params, formfile[] files) throws exception{   
    final string boundary = "---------------------------7da2137580612"; //数据分隔线
    final string endline = "--" + boundary + "--\r\n";//数据结束标志
    
    int filedatalength = 0;
    for(formfile uploadfile : files){//得到文件类型数据的总长度
     stringbuilder fileexplain = new stringbuilder();
     fileexplain.append("--");
     fileexplain.append(boundary);
     fileexplain.append("\r\n");
     fileexplain.append("content-disposition: form-data;name=\""+ uploadfile.getparametername()+"\";filename=\""+ uploadfile.getfilname() + "\"\r\n");
     fileexplain.append("content-type: "+ uploadfile.getcontenttype()+"\r\n\r\n");
     fileexplain.append("\r\n");
     filedatalength += fileexplain.length();
     if(uploadfile.getinstream()!=null){
     filedatalength += uploadfile.getfile().length();
    }else{
    filedatalength += uploadfile.getdata().length;
    }
    }
    stringbuilder textentity = new stringbuilder();
    for (map.entry<string, string> entry : params.entryset()) {//构造文本类型参数的实体数据
      textentity.append("--");
      textentity.append(boundary);
      textentity.append("\r\n");
      textentity.append("content-disposition: form-data; name=\""+ entry.getkey() + "\"\r\n\r\n");
      textentity.append(entry.getvalue());
      textentity.append("\r\n");
    }
    //计算传输给服务器的实体数据总长度
    int datalength = textentity.tostring().getbytes().length + filedatalength + endline.getbytes().length;
    
    url url = new url(path);
    int port = url.getport()==-1 ? 80 : url.getport();
    socket socket = new socket(inetaddress.getbyname(url.gethost()), port);    
    outputstream outstream = socket.getoutputstream();
    //下面完成http请求头的发送
    string requestmethod = "post "+ url.getpath()+" http/1.1\r\n";
    outstream.write(requestmethod.getbytes());
    string accept = "accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
    outstream.write(accept.getbytes());
    string language = "accept-language: zh-cn\r\n";
    outstream.write(language.getbytes());
    string contenttype = "content-type: multipart/form-data; boundary="+ boundary+ "\r\n";
    outstream.write(contenttype.getbytes());
    string contentlength = "content-length: "+ datalength + "\r\n";
    outstream.write(contentlength.getbytes());
    string alive = "connection: keep-alive\r\n";
    outstream.write(alive.getbytes());
    string host = "host: "+ url.gethost() +":"+ port +"\r\n";
    outstream.write(host.getbytes());
    //写完http请求头后根据http协议再写一个回车换行
    outstream.write("\r\n".getbytes());
    //把所有文本类型的实体数据发送出来
    outstream.write(textentity.tostring().getbytes());    
    //把所有文件类型的实体数据发送出来
    for(formfile uploadfile : files){
     stringbuilder fileentity = new stringbuilder();
     fileentity.append("--");
     fileentity.append(boundary);
     fileentity.append("\r\n");
     fileentity.append("content-disposition: form-data;name=\""+ uploadfile.getparametername()+"\";filename=\""+ uploadfile.getfilname() + "\"\r\n");
     fileentity.append("content-type: "+ uploadfile.getcontenttype()+"\r\n\r\n");
     outstream.write(fileentity.tostring().getbytes());
     if(uploadfile.getinstream()!=null){
      byte[] buffer = new byte[1024];
      int len = 0;
      while((len = uploadfile.getinstream().read(buffer, 0, 1024))!=-1){
      outstream.write(buffer, 0, len);
      }
      uploadfile.getinstream().close();
     }else{
      outstream.write(uploadfile.getdata(), 0, uploadfile.getdata().length);
     }
     outstream.write("\r\n".getbytes());
    }
    //下面发送数据结束标志,表示数据已经结束
    outstream.write(endline.getbytes());
    
    bufferedreader reader = new bufferedreader(new inputstreamreader(socket.getinputstream()));
    if(reader.readline().indexof("200")==-1){//读取web服务器返回的数据,判断请求码是否为200,如果不是200,代表请求失败
     return false;
    }
    outstream.flush();
    outstream.close();
    reader.close();
    socket.close();
    return true;
 }
 
 /**
 * 提交数据到服务器
 * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)
 * @param params 请求参数 key为参数名,value为参数值
 * @param file 上传文件
 */
 public static boolean post(string path, map<string, string> params, formfile file) throws exception{
  return post(path, params, new formfile[]{file});
 }
 /**
 * 提交数据到服务器
 * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)
 * @param params 请求参数 key为参数名,value为参数值
 * @param encode 编码
 */
 public static byte[] postfromhttpclient(string path, map<string, string> params, string encode) throws exception{
 list<namevaluepair> formparams = new arraylist<namevaluepair>();//用于存放请求参数
 for(map.entry<string, string> entry : params.entryset()){
  formparams.add(new basicnamevaluepair(entry.getkey(), entry.getvalue()));
 }
 urlencodedformentity entity = new urlencodedformentity(formparams, encode);
 httppost httppost = new httppost(path);
 httppost.setentity(entity);
 httpclient httpclient = new defaulthttpclient();//看作是浏览器
 httpresponse response = httpclient.execute(httppost);//发送post请求 
 return readstream(response.getentity().getcontent());
 }

 /**
 * 发送请求
 * @param path 请求路径
 * @param params 请求参数 key为参数名称 value为参数值
 * @param encode 请求参数的编码
 */
 public static byte[] post(string path, map<string, string> params, string encode) throws exception{
 //string params = "method=save&name="+ urlencoder.encode("老毕", "utf-8")+ "&age=28&";//需要发送的参数
 stringbuilder parambuilder = new stringbuilder("");
 if(params!=null && !params.isempty()){
  for(map.entry<string, string> entry : params.entryset()){
  parambuilder.append(entry.getkey()).append("=")
   .append(urlencoder.encode(entry.getvalue(), encode)).append("&");
  }
  parambuilder.deletecharat(parambuilder.length()-1);
 }
 byte[] data = parambuilder.tostring().getbytes();
 url url = new url(path);
 httpurlconnection conn = (httpurlconnection)url.openconnection();
 conn.setdooutput(true);//允许对外发送请求参数
 conn.setusecaches(false);//不进行缓存
 conn.setconnecttimeout(5 * 1000);
 conn.setrequestmethod("post");
 //下面设置http请求头
 conn.setrequestproperty("accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
 conn.setrequestproperty("accept-language", "zh-cn");
 conn.setrequestproperty("user-agent", "mozilla/4.0 (compatible; msie 8.0; windows nt 5.2; trident/4.0; .net clr 1.1.4322; .net clr 2.0.50727; .net clr 3.0.04506.30; .net clr 3.0.4506.2152; .net clr 3.5.30729)");
 conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");
 conn.setrequestproperty("content-length", string.valueof(data.length));
 conn.setrequestproperty("connection", "keep-alive");
 
 //发送参数
 dataoutputstream outstream = new dataoutputstream(conn.getoutputstream());
 outstream.write(data);//把参数发送出去
 outstream.flush();
 outstream.close();
 if(conn.getresponsecode()==200){
  return readstream(conn.getinputstream());
 }
 return null;
 }
 
 /**
 * 读取流
 * @param instream
 * @return 字节数组
 * @throws exception
 */
 public static byte[] readstream(inputstream instream) throws exception{
 bytearrayoutputstream outsteam = new bytearrayoutputstream();
 byte[] buffer = new byte[1024];
 int len = -1;
 while( (len=instream.read(buffer)) != -1){
  outsteam.write(buffer, 0, len);
 }
 outsteam.close();
 instream.close();
 return outsteam.tobytearray();
 }
}

formfile.java

package cn.itcast.utils;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.inputstream;

/**
 * 上传文件
 */
public class formfile {
 /* 上传文件的数据 */
 private byte[] data;
 private inputstream instream;
 private file file;
 /* 文件名称 */
 private string filname;
 /* 请求参数名称*/
 private string parametername;
 /* 内容类型 */
 private string contenttype = "application/octet-stream";
 
 public formfile(string filname, byte[] data, string parametername, string contenttype) {
 this.data = data;
 this.filname = filname;
 this.parametername = parametername;
 if(contenttype!=null) this.contenttype = contenttype;
 }
 
 public formfile(string filname, file file, string parametername, string contenttype) {
 this.filname = filname;
 this.parametername = parametername;
 this.file = file;
 try {
  this.instream = new fileinputstream(file);
 } catch (filenotfoundexception e) {
  e.printstacktrace();
 }
 if(contenttype!=null) this.contenttype = contenttype;
 }
 
 public file getfile() {
 return file;
 }

 public inputstream getinstream() {
 return instream;
 }

 public byte[] getdata() {
 return data;
 }

 public string getfilname() {
 return filname;
 }

 public void setfilname(string filname) {
 this.filname = filname;
 }

 public string getparametername() {
 return parametername;
 }

 public void setparametername(string parametername) {
 this.parametername = parametername;
 }

 public string getcontenttype() {
 return contenttype;
 }

 public void setcontenttype(string contenttype) {
 this.contenttype = contenttype;
 }
 
}

streamtool.java

package cn.itcast.utils;

import java.io.bytearrayoutputstream;
import java.io.inputstream;

public class streamtool {

 /**
 * 从输入流读取数据
 * @param instream
 * @return
 * @throws exception
 */
 public static byte[] readinputstream(inputstream instream) throws exception{
 bytearrayoutputstream outsteam = new bytearrayoutputstream();
 byte[] buffer = new byte[1024];
 int len = 0;
 while( (len = instream.read(buffer)) !=-1 ){
  outsteam.write(buffer, 0, len);
 }
 outsteam.close();
 instream.close();
 return outsteam.tobytearray();
 }
}

mainactivity.java

package cn.itcast.uploaddata;

import java.io.file;
import java.util.hashmap;
import java.util.map;

import cn.itcast.net.httprequest;
import cn.itcast.utils.formfile;
import cn.itcast.utils.sockethttprequester;
import android.app.activity;
import android.os.bundle;
import android.os.environment;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.toast;

public class mainactivity extends activity {
 private static final string tag = "mainactivity";
  private edittext timelengthtext;
  private edittext titletext;
  private edittext videotext;
  
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    
    button button = (button) this.findviewbyid(r.id.button);
    timelengthtext = (edittext) this.findviewbyid(r.id.timelength);
    videotext = (edittext) this.findviewbyid(r.id.video);
    titletext = (edittext) this.findviewbyid(r.id.title);
    button.setonclicklistener(new view.onclicklistener() {  
  @override
  public void onclick(view v) {
  string title = titletext.gettext().tostring();
  string timelength = timelengthtext.gettext().tostring();
  map<string, string> params = new hashmap<string, string>();
  params.put("method", "save");
  params.put("title", title);
  params.put("timelength", timelength);
  try {
  // httprequest.sendgetrequest("http://192.168.1.100:8080/videoweb/video/manage.do", params, "utf-8");
   file uploadfile = new file(environment.getexternalstoragedirectory(), videotext.gettext().tostring());
   formfile formfile = new formfile("02.mp3", uploadfile, "video", "audio/mpeg");
   sockethttprequester.post("http://192.168.1.100:8080/videoweb/video/manage.do", params, formfile);
   toast.maketext(mainactivity.this, r.string.success, 1).show();
  } catch (exception e) {
   toast.maketext(mainactivity.this, r.string.error, 1).show();
   log.e(tag, e.tostring());
  }
  }
 });
    
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网