java模拟浏览器上传文件
public static void main(String[] args) {
String str = uploadFile("C:/Users/RGKY/Desktop/wKgBHVbLtuWAVNZAAABB_J2qzhc553.jpg", "http://localhost:8087/worldapp/fastfds/uploadSingleFile.cf?fieldName=selectFile", "wKgBHVbLtuWAVNZAAABB_J2qzhc553.jpg");
System.out.println(str);
}
/**
*
* @param file
* 待上传的文件路径
* @param uploadUrl
* 上传服务接口路径
* @param fileName
* 文件名称,服务器获取的文件名称
* @return
*/
public static String uploadFile(/* Bitmap src */String file, String uploadUrl, String fileName) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try {
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
// 允许输入输出流
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 使用POST方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
// name相当于html标签file的name属性,fileName相当于标签value值
dos.writeBytes("Content-Disposition: form-data;name=\"selectFile\";fileName=\"" + fileName + "\"" + end);
dos.writeBytes(end);
// 将要上传的内容写入流中
// InputStream srcis = Function.Bitmap2IS(src);
InputStream srcis = new FileInputStream(file);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
// 读取文件
while ((count = srcis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
srcis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
// 上传返回值
String sl;
String result = "";
while ((sl = br.readLine()) != null)
result = result + sl;
br.close();
is.close();
return result;
} catch (Exception e) {
e.printStackTrace();
return "网络出错!";
}
}
java模拟浏览器上传文件的更多相关文章
- Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)
先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...
- Python模拟浏览器上传文件脚本(Multipart/form-data格式)
http协议本身的原始方法不支持multipart/form-data请求,这个请求由原始方法演变而来的. multipart/form-data的基础方法是post,也就是说是由post方法来组合实 ...
- 《手把手教你》系列技巧篇(五十四)-java+ selenium自动化测试-上传文件-中篇(详细教程)
1.简介 在实际工作中,我们进行web自动化的时候,文件上传是很常见的操作,例如上传用户头像,上传身份证信息等.所以宏哥打算按上传文件的分类对其进行一下讲解和分享. 2.为什么selenium没有提供 ...
- 《手把手教你》系列技巧篇(五十五)-java+ selenium自动化测试-上传文件-下篇(详细教程)
1.简介 在实际工作中,我们进行web自动化的时候,文件上传是很常见的操作,例如上传用户头像,上传身份证信息等.所以宏哥打算按上传文件的分类对其进行一下讲解和分享. 2.为什么selenium没有提供 ...
- ApiPost接口调试工具模拟Post上传文件(中文版Postman)
ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . A ...
- c# 模拟POST上传文件到服务器
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- 使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件
使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件 原来的写法: <input type="file" accept="image/x-png ...
- java web程序上传文件,浏览器显示连接被重置
上传文件时,到13%时浏览器显示连接被重置如图: 参考网上很多方法 比如设置server.xml 的相应大小.时间,然并没有解决问题 connectionTimeout="2000000&q ...
- Java使用HttpURLConnection上传文件
从普通Web页面上传文件非常easy.仅仅须要在form标签叫上enctype="multipart/form-data"就可以,剩余工作便都交给浏览器去完毕数据收集并发送Http ...
随机推荐
- PAT Basic Level 1001
大纲考察内容 数据存储结构:数组.链 基础算法:递归.排序.计算时间复杂度.空间复杂度.分析算法稳定性 1001.害死人不偿命的(3n+1)猜想 (15) https://www.patest.cn/ ...
- AnjularJS系列1 —— 样式相关的指令
最近,开始学习AngularJS. 开始记录学习AngularJS的过程,从一些很简单的知识点开始. 习惯先从实际应用的指令开始,再从应用中去体会AngularJS的优缺点.使用的场景等. 之前一直希 ...
- 4 django系列之HTML通过form标签来同时提交表单内容与上传文件
preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...
- js 闭包
this.color = "blue"; (function(_this) { setInterval(function() { if (_this.color !== " ...
- tensorflow学习
tensorflow安装时遇到gcc: error trying to exec 'as': execvp: No such file or directory. 截止到2016年11月13号,源码编 ...
- 首次接触nodejs
嗯,2017年第一次接触nodejs ,也费了一些时间才终于将hello world正确运行出来. 下面说一下我的详情吧: 第一步:不用说,在https://nodejs.org/en/下载一款新的稳 ...
- 利用Java代码在某些时刻创建Spring上下文
上一篇中,描述了如何使用Spring隐式的创建bean,但当我们需要引进第三方类库添加到我们的逻辑上时,@Conponent与@Autowired是无法添加到类上的,这时,自动装配便不适用了,我们需要 ...
- CSS Sprites优缺点
利用CSS Sprites能很好地减少网页的http请求,从而大大的提高页面的性能,这也是CSS Sprites最大的优点,也是其被广泛传播和应用的主要原因: CSS Sprites能减少图片的字节, ...
- App制作
公司官网生成app: 搜狐快站 http://zhan.sohu.com/
- ObjC运行时部分概念解析(二)
上篇文章简单的说明了两个关键字究竟是什么,这里主要讲讲ObjC中各种基本内存模型 Method typedef struct objc_method *Method; struct objc_meth ...