上传文件类
package com.editxml;
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.UUID;
import android.util.Log;
public class HttpUploader {
private static final String TAG = "uploadFile";
private static final int TIME_OUT = 10 * 1000; // 超时时间
private static final String CHARSET = "utf-8"; // 设置编码
public String reBuff = null;
public int m_DownSign = 0; /**
* android上传文件到服务器
*
* @param filePath
* 需要上传的文件路径+名称
* @param RequestURL
* 请求的rul
* @param sAction
* 请求的动作
* @return 返回响应的内容
*/
public String uploadFile(final String filePath, final String RequestURL,
final String sAction) { new Thread() {
public void run() {
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);
File file = new File(filePath);
if (file != null) {
/**
* 当文件不为空,把文件包装并且上传
*/
DataOutputStream dos = new DataOutputStream(
conn.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END); sb.append("Content-Disposition: form-data; name=\""
+ sAction + "\"; filename=\"" + file.getName()
+ "\"" + LINE_END);
sb.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINE_END);
sb.append(LINE_END);
dos.write(sb.toString().getBytes());
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
}
is.close();
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.e(TAG, "response code:" + res);
if (res == 200) {
Log.e(TAG, "request success");
InputStream input = conn.getInputStream();
StringBuffer sb1 = new StringBuffer();
int ss;
while ((ss = input.read()) != -1) {
sb1.append((char) ss);
}
result = sb1.toString();
reBuff = result;
} else {
Log.e(TAG, "request error");
}
} } catch (MalformedURLException e) {
m_DownSign = -1;
e.printStackTrace();
} catch (IOException e) {
m_DownSign = -1;
e.printStackTrace();
}
m_DownSign = 1;
}
}.start();
while (m_DownSign == 0) {
try {
Thread.sleep(100);
continue;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
} }
return reBuff;
}}
PHP接收文件接口
<?
if(isset( $_FILES) )
{
print("begin...<br>");
print_r( $_FILES );
$name=$_FILES['photo'];
$tmp_name=$_FILES['photo']['tmp_name'];
$sava_name = "D:/AppServ/www/".$_FILES['photo']['name'];
if(move_uploaded_file($tmp_name, $sava_name) )
{
print("ok:$sava_name.<br>");
return 0;
}
}?>
ANDROID调用
HttpUploader uploader = new HttpUploader();
String request = uploader.uploadFile(fDir+"/DownFile/xxxxxx.xml","http://192.168.3.35:80/uploadfile.php","photo");