还是接上篇

修改了一些VideoManageAction

package com.su.action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;
import com.su.action.util.StreamTool;public class VideoManageAction extends ActionSupport {private String title;private String timelength;private File myFile;private String myFileFileName;public String getMethod() {return method;}public void setMethod(String method) {this.method = method;}private String method;public File getMyFile() {return myFile;}public void setMyFile(File myFile) {this.myFile = myFile;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getTimelength() {return timelength;}public void setTimelength(String timelength) {this.timelength = timelength;}public void setMyFileFileName(String myFileFileName) {this.myFileFileName = myFileFileName;}public String getMyFileFileName() {return myFileFileName;}public String execute() throws Exception {if (method.equals("getXml")) {InputStream inStream = ServletActionContext.getRequest().getInputStream();byte[] data = StreamTool.readInputStream(inStream);String xml = new String(data, "UTF-8");System.out.println("11111111111111111111");System.out.println(xml);}if (myFile!=null) {InputStream is = new FileInputStream(myFile);String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");File file = new File(uploadPath);if (!file.exists()) {file.mkdir();}File toFile = new File(uploadPath, this.getMyFileFileName());OutputStream os = new FileOutputStream(toFile);byte[] buffer = new byte[1024];int length = 0;while ((length = is.read(buffer)) > 0) {os.write(buffer, 0, length);}is.close();os.close();}System.out.println(this.getTimelength()+"\n"+this.getTitle()+"\n");return SUCCESS;}
}

这次使用TestCase 用的时候加如activity吧

package cn.itcast.uploaddata;import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;import cn.itcast.net.HttpRequest;
import android.test.AndroidTestCase;
import android.util.Log;public class HttpRequestTest extends AndroidTestCase {private static final String TAG = "HttpRequestTest";public void testSendXMLRequest() throws Throwable{String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><persons><person id=\"23\"><name>liming</name><age>30</age></person></persons>";HttpRequest.sendXML("http://10.1.27.35:8080/VideoWeb2/upload.action?method=getXml", xml);}public void testSendGetRequest() throws Throwable{//?method=save&title=xxxx&timelength=90Map<String, String> params = new HashMap<String, String>();//params.put("method", "save");params.put("title", "liming");params.put("timelength", "80");HttpRequest.sendGetRequest("http://10.1.27.35:8080/VideoWeb2/upload.action", params, "UTF-8");}public void testSendPostRequest() throws Throwable{Map<String, String> params = new HashMap<String, String>();//params.put("method", "save");params.put("title", "中国");params.put("timelength", "80");HttpRequest.sendPostRequest("http://10.1.27.35:8080/VideoWeb2/upload.action", params, "UTF-8");}public void testSendRequestFromHttpClient() throws Throwable{Map<String, String> params = new HashMap<String, String>();//params.put("method", "save");params.put("title", "传智播客");params.put("timelength", "80");boolean result = HttpRequest.sendRequestFromHttpClient("http://10.1.27.35:8080/VideoWeb2/upload.action", params, "UTF-8");Log.i("HttRequestTest", ""+ result);}
}
package cn.itcast.net;import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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;
import org.xmlpull.v1.XmlPullParser;import android.util.Xml;import cn.itcast.utils.StreamTool;public class HttpRequest {public static boolean sendXML(String path, String xml)throws Exception{byte[] data = xml.getBytes();URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(5 * 1000);conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");conn.setRequestProperty("Content-Length", String.valueOf(data.length));OutputStream outStream = conn.getOutputStream();outStream.write(data);outStream.flush();outStream.close();if(conn.getResponseCode()==200){return true;}return false;}public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{StringBuilder sb = new StringBuilder(path);sb.append('?');// ?method=save&title=435435435&timelength=89&for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');}sb.deleteCharAt(sb.length()-1);URL url = new URL(sb.toString());HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);if(conn.getResponseCode()==200){return true;}return false;}public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{// title=dsfdsf&timelength=23&method=saveStringBuilder sb = new StringBuilder();if(params!=null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');}sb.deleteCharAt(sb.length()-1);}byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(5 * 1000);conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据//Content-Type: application/x-www-form-urlencoded//Content-Length: 38conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));OutputStream outStream = conn.getOutputStream();outStream.write(entitydata);outStream.flush();outStream.close();if(conn.getResponseCode()==200){return true;}return false;}//SSL HTTPS Cookiepublic static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception{List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();if(params!=null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据HttpPost post = new HttpPost(path); //formpost.setEntity(entitydata);DefaultHttpClient client = new DefaultHttpClient(); //浏览器HttpResponse response = client.execute(post);//执行请求if(response.getStatusLine().getStatusCode()==200){return true;}return false;}
}

转载于:https://www.cnblogs.com/sfshine/archive/2012/05/14/2742910.html

Struts2+Android (3) 多种方式向服务器发送信息相关推荐

  1. [转]android之Apache Http——向服务器发送请求的同时发送参数

    android之Apache Http--向服务器发送请求的同时发送参数 使用Get方法提交: 其他步骤与上一节的操作相符,只是在传送地址的时候发送参数的格式如下: //Sname和Sage是实际的数 ...

  2. android 拨打电话 发送短信 权限,Android开发实现拨打电话与发送信息的方法分析...

    本文实例讲述了Android开发实现拨打电话与发送信息的方法.分享给大家供大家参考,具体如下: xml布局: android:layout_width="fill_parent" ...

  3. Android之通过向WebService服务器发送XML数据获取相关服务

    原理图如下: 即客户端向WebService服务器通过HTTP协议发送XML数据(内部包含调用的一些方法和相关参数数据),然后WebService服务器给客户端返回一定的XML格式的数据,客户端通过解 ...

  4. Android 通过FTP方式下载服务器文件

    Android中大多数情况都是通过http请求后台数据,这种方式会有很多网络请求框架,现在有个需求是通过请求FTP服务器下载文件,那些经常用到的网络框架都用不了了,接下来我就来实现Android访问F ...

  5. struts2系列(四):struts2国际化的多种方式

    一.struts2国际化原理 根据不同的Locale读取不同的文本. 例如有两个资源文件: 第一个:message_zh_CN.properties 第二个:message_en_US.propert ...

  6. hp服务器通过ilo5安装系统,HPE ProLiant Gen10 通过iLO 5(v1.15) web界面多种方式更新服务器固件,包含升级系统恢复集方法...

    一.iLO web界面固件&操作系统软件界面简单介绍 1.固件 这个界面可以查看服务器安装的固件版本,可以查看以下类型的固件: u电源管理控制器Power Management Control ...

  7. esp8266向服务器发送信息,esp8266发送数据到云服务器

    esp8266发送数据到云服务器 内容精选 换一换 在不使用华为云容器产品的情况下,支持用户在华为云弹性云服务器中部署容器,并实现同一个子网中不同弹性云服务器内的容器相互通信.云服务器内部署容器,容器 ...

  8. wow无法向该服务器发送信息,魔兽世界:玩家无法解决的广告刷屏,却给服务器维护解决了...

    原标题:魔兽世界:玩家无法解决的广告刷屏,却给服务器维护解决了 <魔兽世界>怀旧服战场终于在今天凌晨上线了,为了重温当年在战场厮杀的激情,以及检验自己的PVP技术,一些玩家甚至熬夜守候更新 ...

  9. fiddler如何向服务器发送信息,fiddler 保存请求数据并发送到自己的服务器接口 抓包...

    1.打开fiddler 按Ctrl+r 打开 fiddler script(或者通过菜单Rules 打开Customize Rules) 2.搜索OnBeforeResponse方法,再方法后面添加如 ...

最新文章

  1. java中jar打包的方法
  2. 判断js中的数据类型的几种方法
  3. 常用获取线程基本信息的方法(新手专属)
  4. 996,别让年轻人累到不觉得累
  5. 计算机关机键消失了,如何解决Windows7电脑中的关机键不见了
  6. Docker 镜像基本命令操作
  7. python实现希尔排序(已编程实现)
  8. [WCF编程]10.操作:请求/应答操作
  9. DOS 下修改ip 地址
  10. c语言算法单循环球队比赛安排,单循环赛赛程安排算法的研究.doc
  11. 【元胞自动机】基于matlab元胞自动机交通流模拟仿真【含Matlab源码 1252期】
  12. 独家中文汉化AE脚本 Animation Studio v2.3 Win/Mac一键安装版 预设持续更新 支持CC2020
  13. H3CNE中Vlan间路由
  14. java 继承作用_理解java的三大特性之继承
  15. 盛帮股份深交所上市:市值24亿 赖喜隆父子为实控人
  16. ELK 通过 Rsyslog 收集 HaProxy 日志
  17. 海康机器人工业相机sdk简介
  18. Git的基本使用(用户初始化配置、新建代码库、把文件提交到缓存区、把文件提交到本地仓库等)
  19. 8个实用、强大、超厉害的软件推荐,快收藏吧!
  20. 人力资源管理计算机基础,人力资源管理-专-李佑强-计算机应用基础实践报告.doc...

热门文章

  1. 什么样的网页百度爱收录?
  2. 判断1000素数的c语言程序,C语言求1~1000素数的简单程序
  3. tinyumbrella java_tinyumbrella(小雨伞)
  4. tflearn 数据集太大无法加载进内存问题?——使用image_preloader 或者是 hdf5 dataset to deal with that issue...
  5. mysql 图形化工具
  6. LNMP平台搭建之一:nginx编译安装
  7. C语言memmove()函数: 复制内存内容(可以重叠的内存块)
  8. Mybatis常见面试题(转)
  9. [转]【Git】rebase 用法小结
  10. 初步理解Python进程的信号通讯