当前位置:操作系统 > 安卓/Android >>

android上传文件到服务器

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;
}
}
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,