HttpURLConnection上传文件

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL; import sun.net.www.protocol.http.HttpURLConnection;
/**
* 主要实现文件上传,和接收方绑定接收后信息导入参数传递
* @author zyb
*
*/
public class HttpURLConnectionServices {
/**
* @param fileName 要上传的文件,列:e:/upload/SSD4k对齐分区.zip
* @param Url 上传路径端口号和项目名称,列:http://192.168.1.209:9080/gjbmj
* @param strSiteID 对方的站点编号
* @param strColumnID 对方的栏目编号
* @param strDespatcher 发送信息人
* @param strMechanism 发送信息机构
* @param strOther1
*/
public static void post(String fileName ,String Url,String strSiteID,String strColumnID,
String strDespatcher,String strMechanism,String strOther1) {
try {
String fname =fileName;//要上传的文件
File file = new File(fname);
URL url = new URL(Url+"/cms/infoShare/httpURLConnection.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setChunkedStreamingMode(1024 * 1024);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setConnectTimeout(50000);
conn.setRequestProperty("Content-Type", "multipart/form-data;file="+ file.getName());
conn.setRequestProperty("fileName",file.getName());
conn.setRequestProperty("strSiteID", strSiteID);
conn.setRequestProperty("strColumnID", strColumnID);
conn.setRequestProperty("strDespatcher", strDespatcher);
conn.setRequestProperty("strMechanism", strMechanism);
conn.setRequestProperty("strOther1", strOther1);
conn.connect();
OutputStream out = new DataOutputStream(conn.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[2048];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close(); out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println("---line---"+line);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
new File(fileName).delete();
}
} }

HttpURLConnection上传文件的更多相关文章

  1. java http工具类和HttpUrlConnection上传文件分析

    利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...

  2. Android端通过HttpURLConnection上传文件到服务器

    Android端通过HttpURLConnection上传文件到服务器 一:实现原理 最近在做Android客户端的应用开发,涉及到要把图片上传到后台服务器中,自己选择了做Spring3 MVC HT ...

  3. Android端通过HttpURLConnection上传文件到server

    Android端通过HttpURLConnection上传文件到server 一:实现原理 近期在做Androidclient的应用开发,涉及到要把图片上传到后台server中.自己选择了做Sprin ...

  4. Java使用HttpURLConnection上传文件

    从普通Web页面上传文件非常easy.仅仅须要在form标签叫上enctype="multipart/form-data"就可以,剩余工作便都交给浏览器去完毕数据收集并发送Http ...

  5. Java使用HttpURLConnection上传文件(转)

    从普通Web页面上传文件很简单,只需要在form标签叫上enctype="multipart/form-data"即可,剩余工作便都交给浏览器去完成数据收集并发送Http请求.但是 ...

  6. HttpClient MultipartEntityBuilder 上传文件

    文章转载自: http://blog.csdn.net/yan8024/article/details/46531901 http://www.51testing.com/html/56/n-3707 ...

  7. Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)

    先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...

  8. 【JAVA】通过HttpURLConnection 上传和下载文件(二)

    HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器 上传代码如下: package com.util; import java.i ...

  9. Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:

    Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件: public static String post(String actionU ...

随机推荐

  1. bootstrap设计站点中加入�代码高亮插件

    这款插件的名字叫做google-code-prettify 使用该插件之前的效果: 使用插件之后的效果: 接下来说步骤: (1)下载两个文件 http://codecloud.sinaapp.com/ ...

  2. MsChart<3> 饼状图

    MsChart,饼状图 1 <asp:Chart ID="Chart1" runat="server" BorderlineDashStyle=" ...

  3. iOS开源项目:UIDevice-with-UniqueIdentifier-for-iOS-5

    用于替代系统UDID的方法. https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5 1.使用方法: UILabel *l ...

  4. C语言不定参数

    最近,遇到一个c语言的不定参数问题.其实,对于c语言的不定参数问题,只需要三个函数就可以搞定了.这三个函数的头文件是<stdarg.h>,其实下面的三个函数都是一个宏定义(macro).  ...

  5. informatica powercenter学习笔记(三)

    以前在做DBA时在DB里写过行转列,列转行的CODE.这两天做了一下测试用INFORMATICA来实现行列互换的功能. 列转行的SQL 实现 ENV: RMDB TABLE: SALES STOREN ...

  6. Tensorflow 之 name/variable_scope 变量管理

    name/variable_scope 的作用 充分理解 name / variable_scope TensorFlow 入门笔记 当一个神经网络比较复杂.参数比较多时,就比较需要一个比较好的方式来 ...

  7. SQL CREATE INDEX

    n relational database, Index will be the important mechanism for boosting performance. Index is impo ...

  8. WinForm 窗口缩放动画效果

    using System; using System.Collections.Generic; using System.Text; using System.Threading; using Sys ...

  9. 【精】iOS6 及其以上版本号自己主动旋转、手动强制旋转方案及布局适配

    1.布局适配方式 本文不讨论哪种布局适配方式最好.此处使用的是 Masonry 纯代码布局适配. (Masonry 底层就是 AutoLayout 的 NSLayoutConstraint) 2.iO ...

  10. python urllib2 httplib HTTPConnection

    httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn  ...