用URL请求做文件上传
来源:互联网 发布:程序员很难找女朋友吗 编辑:程序博客网 时间:2024/06/04 19:38
JAVA可以用URL请求直接发送HTTP请求,和web server交互,就和浏览器一样。发送的内容格式是:
假设传的文件名是test.txt,内容是:
content of test.txt
-----------------------------7d65d38307d2 Content-Disposition: form-data; name="myfile"; filename="E:\badboy\test.txt" Content-Type: text/plain content of test.txt -----------------------------7d65d38307d2--
代码概要如下:
URL url = new URL(requestUrl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST"); conn.setDoOutput(true);//perform just like browser does when uploading files, using multipart/form-dataStringBuffer sb = new StringBuffer();sb.append("--"); sb.append(CommonUtil.BOUNDRAY); sb.append("\r\n"); sb.append("Content-Disposition: form-data; name=\"pictureInfo.file\"; filename=\"" + picFileName + "\"\r\n"); sb.append("Content-Type: application/octet-stream\r\n\r\n"); byte[] start_data = sb.toString().getBytes(); byte[] end_data = ("\r\n--" + CommonUtil.BOUNDRAY + "--\r\n").getBytes(); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + CommonUtil.BOUNDRAY); conn.setRequestProperty("Content-Length", String.valueOf(start_data.length + pic_data.length + end_data.length)); //设置内容长度 conn.setConnectTimeout(CommonUtil.NETWORK_TIME_OUT); BufferedOutputStream bos = new BufferedOutputStream(conn.getOutputStream()); bos.write(start_data); bos.write(pic_data); bos.write(end_data); bos.flush(); bos.close(); conn.connect();
- 用URL请求做文件上传
- 使用Window.open(url)做文件下载请求地址
- 获取请求URL、Cookie及文件上传处理
- 使用webuploader做文件上传,跨域请求session不一致
- linux c用url请求下载文件
- 文件上传ajaxfileupload请求
- http请求上传文件
- 用FileUpload做得文件上传
- c#做文件上传
- structs做文件上传
- NSURLSession 做文件上传
- ABAP 上传、下载请求文件
- okhttp的请求+上传文件
- dwz框架做文件上传
- 使用JSP做文件上传
- ftp上传文件&根据url下载文件
- VC++ 用WinInet发送HTTP请求和上传文件
- 模拟浏览器post请求 用java写上传文件后台
- Linux GUI脚本zenity
- Sipdroid注册(三)-注册返回结果的监听
- 计算机图形学 复合变换矩阵
- ExtJS4.0学习心得
- oracle 视图 序列 同义词
- 用URL请求做文件上传
- Linux学习笔记一
- linux下查看文件编码及修改编码
- Weak Alias
- 信号量的实现和应用
- Spring中获得数据库的自增主键值
- 在Magento产品管理中添加额外信息
- zoj 2022||poj 1401 Factorial(阶乘)
- 匿名函数的两个经典错误