LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

android通过http上传文件到web服务器端

admin
2013年2月25日 12:48 本文热度 3821
本文章介绍了一个关于android通过http来实现文件上传功能,在服务器端我们是用php来实现的,有需要的朋友可以参考一下
 代码如下
<?php
///如果有上传文件则接收
if($_FILES){ 
$target_path = $target_path . basename( $_FILES['file1']['name']);
 try{ if(move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['file1']['name']).  " has been uploaded";
}
} catch( Exception $e ) {
echo $e->getMessage();   }
}
?>
Android 代码:
package com.nbcio.baishicha.test;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class test extends Activity {
/*
* 变量声明 newName:上传后在服务器上的文件名称
*
* uploadFile:要上传的文件路径 actionUrl:服务器对应的程序路径
*/
private String newName = "";
private String uploadFile = "";
private String actionUrl = "http://www.hzhuti.com/index.php";//这里定义你的上传路径

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
newName = bundle.getString("fileName");
uploadFile = newName;
try {
String ok = post(actionUrl, newName);
Toast.makeText(this, "OK!", Toast.LENGTH_LONG).show();
finish();

} catch (IOException e) {
//
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/* 上传文件到Server的方法 */
/**
*
* @param actionUrl
* @param params
* @param files
* @return
* @throws IOException
*/
public static String post(String actionUrl, String FileName)
throws IOException {

String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "rn";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
 

URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000);
// 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false); // 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY);
 

DataOutputStream outStream = new DataOutputStream(
conn.getOutputStream());
 

// 发送文件数据
if (FileName != "") {
 

StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name="file1"; filename=""
+ FileName + """ + LINEND);
sb1.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
 

InputStream is = new FileInputStream(FileName);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
 
is.close();
outStream.write(LINEND.getBytes());
 
}
 
// 请求结束标志
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
 
// 得到响应码
int res = conn.getResponseCode();
InputStream in = null;
if (res == 200) {
in = conn.getInputStream();
int ch;
StringBuilder sb2 = new StringBuilder();
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
}
return in == null ? null : in.toString();
}
}

该文章在 2013/2/25 12:48:32 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved