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

android上传文件到服务器

2015-12-07 16:41 399 查看
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

public class UploadThread extends Thread {
private String fileName;
private String url;
public static final String BOUNDARY = "---------------------------61132773524357";
public static final String PREFIX = "--";
public static final String END = "\r\n";

public UploadThread(String fileName, String url) {
super();
this.fileName = fileName;
this.url = url;
}

@Override
public void run() {
// TODO Auto-generated method stub
DataOutputStream dos = null;
FileInputStream fis = null;
BufferedReader br = null;
try {
URL httpUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(PREFIX+BOUNDARY+END);
String uploadFileName = UUID.randomUUID().toString() + "." + fileName.substring(fileName.lastIndexOf(".")+1);
dos.writeBytes("Content-Disposition: form-data; name=\"myfile\";"
+ " filename=\""+uploadFileName+"\"" + END);
dos.writeBytes(END);
fis = new FileInputStream(new File(fileName));
byte[] b = new byte[1024*4];
int len;
while((len=fis.read(b)) != -1) {
dos.write(b, 0, len);
}
dos.writeBytes(END);
dos.writeBytes(PREFIX+BOUNDARY+PREFIX+END);
dos.flush();

br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String str;
while((str=br.readLine()) != null) {
sb.append(str);
}

System.out.println("response:" + sb.toString());

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}


这里使用到的php服务器代码如下:

<?php
if(empty($_FILES)) {
echo "上传失败";
exit();
}
//判断文件大小是否小于2M,如果大于2M,那么久不上传
$file_size=$_FILES['myfile']['size'];
if($file_size>2*1024*1024) {
echo "文件大小必须 > 2M";
exit();
}
//is_uploaded_file — 判断文件是否是通过 HTTP POST 上传的
//如果 filename 所给出的文件是通过 HTTP POST 上传的则返回 TRUE。这可以用来确保恶意的用户无法欺骗脚本去访问本不能访问的文件,例如 /etc/passwd
if(is_uploaded_file($_FILES['myfile']['tmp_name'])) {
//获取上传的文件
$uploaded_file = $_FILES['myfile']['tmp_name'];
//定义需要上传到服务器的哪一个位置
$move_to_file = $_SERVER['DOCUMENT_ROOT']."/file/".$_FILES['myfile']['name'];
//如果成功移动到服务器的具体位置,那么上传成功,否则失败
//为了防止上传中出现中文乱码导致上传失败,这里使用了iconv函数
if(move_uploaded_file($uploaded_file, iconv("utf-8","gb2312",$move_to_file))) {
echo $_FILES['myfile']['tmp_name']."上传成功";
} else {
echo $_FILES['myfile']['tmp_name']."上传失败";
}
} else {
echo $_FILES['myfile']['tmp_name']."上传失败";
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: