安卓上传图片 c#作为服务器端代码怎么写

vs_shell 2014-08-12 03:36:13
* 上传文件至Server的方法 */
private void uploadFile() {

String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

try
{

URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设置DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end );


ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end);

/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int length = -1;
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

/* close streams */
fStream.close();
ds.flush();

/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog("上传成功"+b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog("上传失败"+e);
}

}
/* 显示Dialog的method */
private void showDialog(String mess) {
AlertDialog.Builder(Index_Activity.this).setTitle("提示")
.setMessage(mess)
.setNegativeButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}

安卓上传代码是这样写的,不知有不有问题 还有服务器c#代码怎么写
...全文
213 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
DrSmart 2014-08-13
  • 打赏
  • 举报
回复
发错区了吧,上传和android无关的
vs_shell 2014-08-13
  • 打赏
  • 举报
回复
那上面脚本正确不,麻烦帮忙检查一下
licai1210 2014-08-12
  • 打赏
  • 举报
回复
客户端只要提交格式正确,服务器你直接用Request.Files就可以了啊,这事情我做的多。
vs_shell 2014-08-12
  • 打赏
  • 举报
回复
服务器要求用c#代码编写
lionfresh 2014-08-12
  • 打赏
  • 举报
回复
这和服务端用什么写有什么关系呢?你想用什么语言就按那种语言的来写就是。
vs_shell 2014-08-12
  • 打赏
  • 举报
回复
大神进来呀!

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧