50,539
社区成员
发帖
与我相关
我的任务
分享
private String UploadVideo(Map<String, String> tmpMap,String tPathName) throws ClientProtocolException, IOException {
// 返回tETag
String tETag = "";
// 连接
HttpURLConnection conn = null;
// 传递参数
OutputStreamWriter tStreamWriter = null;
// 接受参数
InputStream tInputStream = null;
String nextLine = "\r\n";
String dividerStart = "--";
String BOUNDARY = "******8888668ec";
try{
URL url = new URL(tmpMap.get("url"));
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");// 提交模式
conn.setConnectTimeout(DI_CommonType.ConnectTimeout);// 连接超时 单位毫秒
conn.setDoOutput(true);// 是否输入参数
conn.setDoInput(true); // 是否接收参数
conn.setUseCaches(false);// 是否缓存
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset","UTF-8");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
// 分界符
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// 定义写入数据流
DataOutputStream tDataOutputStream = new DataOutputStream(conn.getOutputStream());
tDataOutputStream.writeBytes(dividerStart + BOUNDARY + nextLine);
//设置与上传文件相关的信息
tDataOutputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ tFilePathc.substring(tFilePathc.lastIndexOf("/") + 1) + "\"" + nextLine);
tDataOutputStream.writeBytes(nextLine);
FileInputStream fis = new FileInputStream(tFilePathc);
byte[] buffer = new byte[1024 * 32];
int count;
// 读取文件内容,并写入OutputStream对象
while ((count = fis.read(buffer)) != -1) {
tDataOutputStream.write(buffer, 0, count);
}
fis.close();
tDataOutputStream.writeBytes(nextLine);
tDataOutputStream.writeBytes(dividerStart + BOUNDARY + dividerStart + nextLine);
tDataOutputStream.flush();
// 接收参数
int tResponseCode = conn.getResponseCode();
Map<String,List<String>> headerFields = conn.getHeaderFields();
System.out.println(headerFields.get("ETag"));
if (200==tResponseCode) {
tETag = headerFields.get("ETag").toString();
}
}catch(IOException e){
try {
System.out.println(conn.getResponseCode());
} catch (IOException e1) {
e1.printStackTrace();
}
// e.printStackTrace();
System.out.println("通信异常!+++");
}finally {
try {
if (null!=tStreamWriter) {
tStreamWriter.close();
}
if (null!=tInputStream) {
tInputStream.close();
}
if (null!=conn) {
conn.disconnect();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return tETag;
}