- 浏览: 303754 次
- 性别:
- 来自: 河南开封
文章分类
最新评论
-
keven_niu:
好不错哦。。
jquery dialog对话框插件实例弹层效果 -
我是云:
<div class="quote_title ...
Java switch-case语句用法 -
ytt06460105:
问题已解决。
使用JDBC访问DB2的问题:no db2jdbc in java.library.path -
ytt06460105:
楼主:按照你的方法做了,又出现了新的错误:[com.aliba ...
使用JDBC访问DB2的问题:no db2jdbc in java.library.path -
zjuttsw:
<div class="quote_title ...
Java switch-case语句用法
Java代码
Java代码
public class FormFile { // 上传文件的数据 private byte[] data; private InputStream inStream; // 文件名称 private String filename; // 表单名称 private String formname; // 内容类型 private String contentType = "application/octet-stream"; public FormFile(String filename, byte[] data, String formname, String contentType) { this.data = data; this.filename = filename; this.formname = formname; if (contentType != null) { this.contentType = contentType; } } public FormFile(String filename, InputStream inStream, String formname, String contentType) { this.filename = filename; this.formname = formname; this.inStream = inStream; if (contentType != null) { 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 getFormname() { return formname; } public void setFormname(String formname) { this.formname = formname; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } } public class FormFile { // 上传文件的数据 private byte[] data; private InputStream inStream; // 文件名称 private String filename; // 表单名称 private String formname; // 内容类型 private String contentType = "application/octet-stream"; public FormFile(String filename, byte[] data, String formname, String contentType) { this.data = data; this.filename = filename; this.formname = formname; if (contentType != null) { this.contentType = contentType; } } public FormFile(String filename, InputStream inStream, String formname, String contentType) { this.filename = filename; this.formname = formname; this.inStream = inStream; if (contentType != null) { 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 getFormname() { return formname; } public void setFormname(String formname) { this.formname = formname; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } }
Java代码
public class HttpUploadRequester { /** * 直接通过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 actionUrl * 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器, * 你可以使用http://192.168.1.10:8080这样的路径测试) * @param params * 请求参数 key为参数名,value为参数值 * @param file * 上传文件 */ // http协议中分割符,随便定义 private static final String HTTP_BOUNDARY = "---------9dx5a2d578c2"; private static final String MULTIPART_FORM_DATA = "multipart/form-data"; private static final String LINE_ENTER = "\r\n"; // 换行 回车 private static final int RESPONSE_OK = 200; public static String post(String urlPath, Map<String, String> params, FormFile[] formFiles) { try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setReadTimeout(5 * 1000); conn.setDoOutput(true); // 发送POST请求, 必须设置允许输出 conn.setUseCaches(false); conn.setRequestProperty("Connection", "Keep-Alive"); // 维持长链接 conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + HTTP_BOUNDARY); StringBuilder formItemData = new StringBuilder(); // 构建 表单字段内容 for (Map.Entry<String, String> entry : params.entrySet()) { formItemData.append("--"); formItemData.append(HTTP_BOUNDARY); formItemData.append(LINE_ENTER); formItemData.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n"); formItemData.append(entry.getValue()); formItemData.append(LINE_ENTER); } DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); // 发送表单字段内容到服务器 outStream.write(formItemData.toString().getBytes()); // 发送上传文件数据 for (FormFile fileData : formFiles) { StringBuilder fileSplit = new StringBuilder(); fileSplit.append("--"); fileSplit.append(HTTP_BOUNDARY); fileSplit.append(LINE_ENTER); fileSplit.append("Content-Disposition: form-data;name=\"" + fileData.getFormname() + "\";filename=\"" + fileData.getFilename() + "\"\r\n"); fileSplit.append("Content-Type:" + fileData.getContentType() + LINE_ENTER + LINE_ENTER); outStream.write(fileSplit.toString().getBytes()); if (fileData.getInStream() != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = fileData.getInStream().read()) != -1) { outStream.write(buffer, 0, length); } fileData.getInStream().close(); } else { outStream.write(fileData.getData(), 0, fileData.getData().length); } outStream.write(LINE_ENTER.getBytes()); } // 数据结束标志 byte[] endData = ("--" + HTTP_BOUNDARY + "--" + LINE_ENTER) .getBytes(); outStream.write(endData); outStream.flush(); outStream.close(); int responseCode = conn.getResponseCode(); if (responseCode != RESPONSE_OK) { throw new RuntimeException("请求url失败"); } InputStream is = conn.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } Log.i("HttpPost", b.toString()); conn.disconnect(); return b.toString(); } catch (Exception e) { throw new RuntimeException(); } } // 上传单个文件 public static String post(String urlPath, Map<String, String> params, FormFile formFiles) { return post(urlPath, params, new FormFile[] { formFiles }); } } public class HttpUploadRequester { /** * 直接通过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 actionUrl * 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器, * 你可以使用http://192.168.1.10:8080这样的路径测试) * @param params * 请求参数 key为参数名,value为参数值 * @param file * 上传文件 */ // http协议中分割符,随便定义 private static final String HTTP_BOUNDARY = "---------9dx5a2d578c2"; private static final String MULTIPART_FORM_DATA = "multipart/form-data"; private static final String LINE_ENTER = "\r\n"; // 换行 回车 private static final int RESPONSE_OK = 200; public static String post(String urlPath, Map<String, String> params, FormFile[] formFiles) { try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setReadTimeout(5 * 1000); conn.setDoOutput(true); // 发送POST请求, 必须设置允许输出 conn.setUseCaches(false); conn.setRequestProperty("Connection", "Keep-Alive"); // 维持长链接 conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + HTTP_BOUNDARY); StringBuilder formItemData = new StringBuilder(); // 构建 表单字段内容 for (Map.Entry<String, String> entry : params.entrySet()) { formItemData.append("--"); formItemData.append(HTTP_BOUNDARY); formItemData.append(LINE_ENTER); formItemData.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n"); formItemData.append(entry.getValue()); formItemData.append(LINE_ENTER); } DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); // 发送表单字段内容到服务器 outStream.write(formItemData.toString().getBytes()); // 发送上传文件数据 for (FormFile fileData : formFiles) { StringBuilder fileSplit = new StringBuilder(); fileSplit.append("--"); fileSplit.append(HTTP_BOUNDARY); fileSplit.append(LINE_ENTER); fileSplit.append("Content-Disposition: form-data;name=\"" + fileData.getFormname() + "\";filename=\"" + fileData.getFilename() + "\"\r\n"); fileSplit.append("Content-Type:" + fileData.getContentType() + LINE_ENTER + LINE_ENTER); outStream.write(fileSplit.toString().getBytes()); if (fileData.getInStream() != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = fileData.getInStream().read()) != -1) { outStream.write(buffer, 0, length); } fileData.getInStream().close(); } else { outStream.write(fileData.getData(), 0, fileData.getData().length); } outStream.write(LINE_ENTER.getBytes()); } // 数据结束标志 byte[] endData = ("--" + HTTP_BOUNDARY + "--" + LINE_ENTER) .getBytes(); outStream.write(endData); outStream.flush(); outStream.close(); int responseCode = conn.getResponseCode(); if (responseCode != RESPONSE_OK) { throw new RuntimeException("请求url失败"); } InputStream is = conn.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } Log.i("HttpPost", b.toString()); conn.disconnect(); return b.toString(); } catch (Exception e) { throw new RuntimeException(); } } // 上传单个文件 public static String post(String urlPath, Map<String, String> params, FormFile formFiles) { return post(urlPath, params, new FormFile[] { formFiles }); } } Java代码 // 以表单方式发送请求 public static void sendDataToServerByForm() throws Exception { Map<String, String> params = new HashMap<String, String>(); params.put("method", "sendDataByForm"); params.put("strData", "字符串数据"); // 获取SDCard中的good.jpg File file = new File(Environment.getExternalStorageDirectory(), "app_Goog_Android_w.png"); FormFile fileData = new FormFile("app_Goog_Android_w.png", new FileInputStream(file), "fileData", "application/octet-stream"); HttpUploadRequester.post( "http://192.168.0.2:8080/AndroidWebServer/server.do", params, fileData); }
发表评论
-
log4j.properties配置详解
2011-12-02 17:53 752Log4J的配置文件(Configuration File)就 ... -
Spring MVC 介绍
2011-09-21 13:53 1811大部分的Java应用都是基于Web的,作为轻量级的JavaEE ... -
WdatePicker日历控件使用方法
2011-07-22 17:54 936http://www.uxiu.com/templates/u ... -
struts2中<s:bean name="org.apache.struts2.util.Counter" id="counter">循环标签的使用
2011-07-13 17:38 3138struts2随提供了循环控制标签<s:iterator ... -
jquery dialog对话框插件实例弹层效果
2011-06-10 11:23 8075本文章是利用了jquery dia ... -
三大框架的技术起源
2010-08-23 13:45 1060Struts、Hibernate和Spring是 ...
相关推荐
1. testhttppost.exe:http post上传文件工具; 2. TestHttpPost: 源码; 3.lr_post.txt: 录制的loadrunner http post上传文件到服务器的示例; 4. getjpg.v bs: 读出一个图片文件,将其内容转换为loadrunner ...
Base64工具类-字符编码工具类-数据类型转换-日期工具类-Escape中文转码工具类-fastjson工具类-文件工具类-Http工具类-http请求工具类-用于模拟HTTP请求中GET/POST方式 -图片处理工具类-Ip工具类-mail工具类-Map工具...
该工具类使用httpclient进行http or https请求,包括requestbody格式和form表单格式,另外含文件服务器中转上传方法,几乎支持所有常用接口调用,内含详细注释和说明文件,含jar包,及maven方式引用,拿过去直接用吧
http请求提交类工具类,可以用main方法直接测试
模拟http请求实现文件上传,模拟doGet、doPost请求,实现信息发送
用来测试rest的一个工具类,我觉得比较好的是可以发送文件
1.稍微修改可以实现自动上传文件 2.上传文件不受大小限制 ...文件上传过程中可以显示当前上传进度,当前平均上传速度、上传文件已花费时长 上传文件大小和总的文件大小百分比 4。保证文件的稳定传输
java上传文件工具类 很全面
通用的http请求工具,可以模拟表单提交、上传文件、发送请求、获取网页内容
Android 上传文件至服务器和下载文件至本地,亲测有效,只需传入相关参数即可。/** * android上传文件到服务器 * * @param file * 需要上传的文件 * @param RequestURL * 请求的rul * @return 返回...
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/775753
浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具common-fileupload这个文件上传组件。
包括编译器、构建工具(如Make、Gradle、Maven)等,用于将源代码转换为可执行文件或库,并进行资源打包、优化等处理。 调试与测试: 集成调试器允许开发者逐行执行代码,设置断点、查看变量值、跟踪调用堆栈等...
Chrome扩展程序会记录浏览器发送的所有HTTP/S请求和实际用户交互,创建同步的JMeter和Selenium脚本,然后自动将其上传到BlazeMeter,您可以在其中单击执行它。无需安装JMeter或Selenium即可记录或运行性能测试,...
一个基于JavaFX图形框架构建的简易Http请求工具,目前实现了GET, POST, PUT, DELETE四种Http请求方式 ...GET, DELETE请求不支持发送请求体以及上传文件 历史记录信息存储在~/PostJFX/logs/log.db中
MD5、RSA、Base64Utils、DialogUtil、ZXingUtils、GlideUtil、、、工具类 自己总结了一些必备的工具类,可直接放入工程,简单调用 对OkHttp进行了封装,简单调用,便于操作 进行了post、get、文件上传等进行封装
该代码完整的实现了shiyonglayui技术实现多文件的上传预览和删除功能,下载下来直接导入到开发工具中就可以使用
ftp ip 端口 用于上传文件至服务器或进行文件操作,默认端口为21。bin指用二进制方式传送(可执行文件进);默认为ASCII格式传送(文本文件时) route print 显示出IP路由,将主要显示网络地址Network addres,子网...
本源码包含公众号菜单初始化开发,菜单事件开发,普通token和网页授权token开发工具类,js_ticket获取和缓存,获取openid开发,公众号网络请求封装,xml格式转换工具类,文件上传工具类,关注事件/取消关注事件开发...
请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则...