您的位置:首页 > 移动开发 > Android开发

android上传图片文件至C#服务器

2015-08-09 09:20 911 查看
这几天开发安卓项目,需要上传文件到服务器,一开始一点不会,然后就开始了查资料模式,中间走了许多弯路,不过功夫不负有心人,终于实现了选择相册图片上传和拍照上传,还理解了http协议,现在我把我的经验分享给大家,希望对大家有帮助。

主要代码:

//异步加载,千万不能把网络请求放在UI主线程中,不然会发生异常                android.os.NetworkOnMainThreadException
String end = "\r\n";//回车换行
String twoHyphens = "--";//参数分隔符ps:与boundary分割传入的参数
String boundary = "***********";//分界线可以任意分配
try {
URL url = new URL(urlStr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);//不使用缓存
httpURLConnection.setRequestMethod("POST");//post请求
httpURLConnection.setRequestProperty("Connection","keep-Alive");//一直保持链接状态
httpURLConnection.setRequestProperty("Charset", "utf-8");//字符集编码为utf-8
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
//请求数据为多元性数据,这里只用分界线不用分隔符表示,必须严格按照这样写,不然服务器无法识别
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
//获取输出流
String newName = "head.jpg";//临时文件名字。可任意改.我的服务器端存储的不是它,以为我用了全球唯一标识符(Guid)来命名的
dataOutputStream.writeBytes(twoHyphens+boundary+end);
dataOutputStream.writeBytes("Content-Disposition: form-data; "+
"name=\"MyHeadPicture\";filename=\""+
newName +"\""+ end);
/*注意,千万注意这的MyHeadPicture与浏览器端的<input type="file" name="MyHeadPicture"/>name对应的属性一致
,记住不能少了回车换行结束标志*/
dataOutputStream.writeBytes(end);
FileInputStream fStream =new FileInputStream(file);//获取本地文件输入流
/* 设置每次写入1024bytes */
int bufferSize =1024;
byte[] buffer =new byte[bufferSize];
int length =-1;
//      StringBuffer sb = new StringBuffer();
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) !=-1)
{
//         sb.append(length);
/* 将资料写入DataOutputStream中 */
dataOutputStream.write(buffer, 0, length);//将文件一字节流形式输入到输出流中
}
dataOutputStream.writeBytes(end);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
dataOutputStream.flush();
/* 取得Response内容 */
InputStream is = httpURLConnection.getInputStream();//服务器端响应
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) !=-1 )
{
b.append( (char)ch );
}
System.err.println(b.toString());
dataOutputStream.close();
return b.toString();//返回响应内容
} catch (IOException e) {
e.printStackTrace();
}


初始界面:



图片选择界面:



照相:



选择图片成功界面:



源码下载地址

更多博主分享请关注:

手机弹幕实现

贪吃蛇源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: