package com.itrackstar.tcube.common;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;
import android.util.Log;
public class PostFile
{
private static final String TAG = "uploadFile";
private static final int TIME_OUT = 10 * 1000; // 超時時間
private static final String CHARSET = "utf-8"; // 設置編碼
/**
*
* android上傳文件到服務器
*
* @param file
* 需要上傳的文件
*
* @param RequestURL
* 請求的rul
*
* @return 返回響應的內容
*/
public static String uploadFile(File file, Map<String, String> params, String RequestURL)
{
String result = null;
String BOUNDARY = UUID.randomUUID().toString(); // 邊界標識 隨機生成
String PREFIX = "--", LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; // 內容類型
try
{
URL url = new URL( RequestURL );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout( TIME_OUT );
conn.setConnectTimeout( TIME_OUT );
conn.setDoInput( true ); // 允許輸入流
conn.setDoOutput( true ); // 允許輸出流
conn.setUseCaches( false ); // 不允許使用緩存
conn.setRequestMethod( "POST" ); // 請求方式
conn.setRequestProperty( "Charset", CHARSET ); // 設置編碼
conn.setRequestProperty( "connection", "keep-alive" );
conn.setRequestProperty( "Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY );
DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
if (file != null)
{
/**
*
* 當文件不為空,把文件包裝並且上傳
*/
InputStream is = new FileInputStream( file );
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read( bytes )) != -1)
{
dos.write( bytes, 0, len );
Log.i( "bytetag", bytes.toString() );
}
is.close();
StringBuilder sbs = new StringBuilder();
//上傳的表單參數部分,格式請參考文章
for ( Map.Entry<String, String> entry : params.entrySet() )
{//構建表單字段內容
sbs.append( PREFIX );
sbs.append( BOUNDARY );
sbs.append( LINE_END );
sbs.append( "Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINE_END + LINE_END );
sbs.append( entry.getValue() );
sbs.append( LINE_END );
}
sbs.append( PREFIX );
sbs.append( BOUNDARY );
sbs.append( LINE_END );
sbs.append( "Content-Disposition: form-data; name=\"filelength\"" + LINE_END + LINE_END );
sbs.append( file.length() );
sbs.append( LINE_END );
dos.write( sbs.toString().getBytes() );//發送表單字段數據
dos.write( LINE_END.getBytes() );
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes();
dos.write( end_data );
dos.flush();
/**
*
* 獲取響應碼 200=成功
*
* 當響應成功,獲取響應的流
*/
int res = conn.getResponseCode();
Log.i( TAG, "response code:" + res );
if (res == 200)
{
Log.e( TAG, "request success" );
InputStream input = conn.getInputStream();
int total = input.available();
byte[] bs = new byte[total];
input.read( bs );
result = new String( bs, "GBK" );
Log.i( TAG, "result : " + result );
}
else
{
Log.i( TAG, "request error" );
}
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return result;
}
}