Android要实现文件上传,可以利用Socket上传,也可以模拟Web进行上传,但是如果是使用第一种方式上传,严格的话就得使用TCP,这样容易生成系统死掉,或者是长时间等待,如果是UDP来传,就容易造成数据丢失,因此在这里选择了Web进行上传,使用Web进行上传是模拟的Http Post上传数据,当然,Post上传数据的类,在网上找了一找,方式虽然很多,但是没有一个感觉是我所使用的,所以参照原理之类的,进行了一下修改,算是做了一个参考。并且利用这个类完成了文件和表彰的上传服务。
上传方法一:
public void uploadFile(){
String name=URLEncoder.encode(text1.getText().toString(),"utf-8");
String pass=URLEncoder.encode(text2.getText().toString(),"utf-8");
Map params = new HashMap();
params.put("NAME", name);
params.put("PASSWORD", pass);
post("http://192.168.1.120/dev/index.php/Device/UploadFile?filename=111&filetype=IMAGE", params, upfiles);
}
Map upfiles = new HashMap();
void getFile() {
File file = new File("/sdcard/");
File[] files = file.listFiles(new fileFilter());
for (File f: files) {
upfiles.put(f.getName(), new File("/sdcard/"+f.getName()));
}
// Toast.makeText(this, filename, Toast.LENGTH_LONG).show();
}
class fileFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
return filename.endsWith(".mp3");
}
}
// 上传代码,第一个参数,为要使用的URL,第二个参数,为表单内容,第三个参数为要上传的文件,可以上传多个文件,这根据需要页定
public static boolean post(String actionUrl,Map params,Map files) throws IOException {
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
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"); // Post方式
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY);
// 首先组拼文本类型的参数
StringBuilder sb = new StringBuilder();
for (Map.Entry entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\""
+ entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}
DataOutputStream outStream = new DataOutputStream(
conn.getOutputStream());
outStream.write(sb.toString().getBytes());
// 发送文件数据
if (files != null)
for (Map.Entry file : files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""
+ file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: multipart/form-data; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(file.getValue());
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();
// 得到响应码
boolean success = conn.getResponseCode()==200;
InputStream in = conn.getInputStream();
InputStreamReader isReader = new InputStreamReader(in);
BufferedReader bufReader = new BufferedReader(isReader);
String line = null;
String data = "getResult=";
while ((line = bufReader.readLine()) != null)
data += line;
outStream.close();
conn.disconnect();
return success;
}
上传方法二:
final String ENCORDING="UTF-8";
public boolean upload(String filepath) throws Exception {
String boundary = "---------------------------7db1c523809b2";//+java.util.UUID.randomUUID().toString();//
// 分割线
File file = new File(filepath);
String fileName=new String("哈哈嗨".getBytes(),"ISO-8859-1");
// 用来解析主机名和端口
URL url = new URL("http://192.168.1.120/dev/index.php/Device/UploadFile?filename="+fileName+"&filetype=IMAGE");
// 用来开启连接
StringBuilder sb = new StringBuilder();
// 用来拼装请求
/*// username字段
sb.append("--" + boundary + "\r\n");
sb.append("Content-Disposition: form-data; name=\"username\"" + "\r\n");
sb.append("\r\n");
sb.append(username + "\r\n");
// password字段
sb.append("--" + boundary + "\r\n");
sb.append("Content-Disposition: form-data; name=\"password\"" + "\r\n");
sb.append("\r\n");
sb.append(password + "\r\n");*/
// 文件部分
sb.append("--" + boundary + "\r\n");
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + filepath + "\"" + "\r\n");
sb.append("Content-Type: application/octet-stream" + "\r\n");
sb.append("\r\n");
// 将开头和结尾部分转为字节数组,因为设置Content-Type时长度是字节长度
byte[] before = sb.toString().getBytes(ENCORDING);
byte[] after = ("\r\n--" + boundary + "--\r\n").getBytes(ENCORDING);
// 打开连接, 设置请求头
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("Content-Length", before.length + file.length() + after.length + "");
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取输入输出流
OutputStream out = conn.getOutputStream();
FileInputStream fis = new FileInputStream(file);
// 将开头部分写出
out.write(before);
// 写出文件数据
byte[] buf = new byte[1024*5];
int len;
while ((len = fis.read(buf)) != -1)
out.write(buf, 0, len);
// 将结尾部分写出
out.write(after);
InputStream in = conn.getInputStream();
InputStreamReader isReader = new InputStreamReader(in);
BufferedReader bufReader = new BufferedReader(isReader);
String line = null;
String data = "getResult=";
while ((line = bufReader.readLine()) != null)
data += line;
Log.e("fromServer", "result="+data);
boolean sucess=conn.getResponseCode() == 200;
in.close();
fis.close();
out.close();
conn.disconnect();
return sucess;
}
通过此代码就可以实现将内容保存到SD卡等设备上,当然要使用网络,必须得有网络的访问权限。这个需要自己添加,在这里不再添加!