android上传文件到wamp服务器
1.php server(wamp)部分
建立unload.php页面代码如下
<?php move_uploaded_file($_FILES["file1"]["tmp_name"],
"upload/" . $_FILES["file1"]["name"]);
echo "存储在: " . "upload/" . $_FILES["file1"]["name"];
?>
需要配置c:\windows\temp的目录权限,user group有权限写

另外ip访问设置,wamp中的c:\wamp\alias\adt.conf
添加Allow from 192.168.0.102 指定IP
2.android部分
Main.java
package com.test; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map; import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast; public class Main extends Activity {
/*
* 变量声明 newName:上传后在服务器上的文件名称 uploadFile:要上传的文件路径 actionUrl:服务器上对应的程序路径
*/
private String newName = "pp.jpg";
private String uploadFile = "/data/pp.jpg";
private String actionUrl = "http://192.168.0.102/tp/upload.php";
private TextView mText1;
private TextView mText2;
private Button mButton;
private String msg_result = null;
public static final int REFRESH = 0x000001;
private static final int FILE_SELECT_CODE = 1 ;
protected static final int PROGRESS_VISIBLE = 10;
protected static final int PROGRESS_INVISIBLE = 11;
private Handler mHandler = null; private ProgressBar progressBar1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); progressBar1 = (ProgressBar)findViewById(R.id.progress_horizontal); mText1 = (TextView) findViewById(R.id.myText2);
mText1.setText("文件路径:\n" + uploadFile);
mText2 = (TextView) findViewById(R.id.myText3);
mText2.setText("上传网址:\n" + actionUrl);
/* 设置mButton的onClick事件处理 */
mButton = (Button) findViewById(R.id.myButton);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//showFileChooser();
File f = new File(uploadFile);
if (!f.exists()) {
showDialog(uploadFile + "\r\n文件不存在");
return;
}
new MyThread().start();
}
}); mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == REFRESH) { showDialog(msg_result);
}
else if (msg.what == PROGRESS_VISIBLE){
progressBar1.setVisibility(View.VISIBLE);
}
else if (msg.what == PROGRESS_INVISIBLE){
progressBar1.setVisibility(View.INVISIBLE);
}
super.handleMessage(msg);
}
};
} /* 上传文件至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;
/* 从文件读取数据至缓冲区 */
int sum = 0; Message msg = new Message();
msg.what = PROGRESS_VISIBLE;
mHandler.sendMessage(msg);
while ((length = fStream.read(buffer)) != -1) {
sum += length;
/* 将资料写入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();
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] by = new byte[1000];
int n = 0;
while ((n = is.read(by)) != -1) {
bos.write(by, 0, n);
} msg_result = bos.toString();
/* 关闭DataOutputStream */
ds.close(); msg = new Message();
msg.what = PROGRESS_INVISIBLE;
mHandler.sendMessage(msg); } catch (Exception e) {
msg_result = e.getMessage();
}
} /* 显示Dialog的method */
private void showDialog(String mess) {
new AlertDialog.Builder(Main.this).setTitle("Message1")
.setMessage(mess)
.setNegativeButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
} public class MyThread extends Thread {
public void run() {
uploadFile();
Message msg = new Message();
msg.what = REFRESH;
mHandler.sendMessage(msg);
}
}
}
layout的main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title"
android:textSize="20sp"
android:textColor="@drawable/black"
android:layout_x="10px"
android:layout_y="12px"
>
</TextView>
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="10px"
android:layout_y="52px"
>
</TextView>
<TextView
android:id="@+id/myText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="10px"
android:layout_y="102px"
>
</TextView>
<Button
android:id="@+id/myButton"
android:layout_width="92px"
android:layout_height="49px"
android:text="@string/str_button"
android:textSize="15sp"
android:layout_x="90px"
android:layout_y="170px"
>
</Button> <ProgressBar
android:id="@+id/progress_horizontal"
android:layout_width="292px"
android:layout_height="49px"
android:layout_x="90px"
android:layout_y="250px"
android:max="100"
android:progress="0"
android:secondaryProgress="0"
android:visibility="invisible" /> </AbsoluteLayout>
AndroidManifest.xml需要添加权限
<uses-permission android:name="android.permission.INTERNET" />
事例


android上传文件到wamp服务器的更多相关文章
- Android 上传文件到 FTP 服务器
实现背景 近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然之前接触过FTP服务器,了解基本的使用流程,但是将此流程从使用习惯转化为代码实现还是有一定难度的.但是基本的流程还是 ...
- android上传文件到服务器
package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...
- android -上传文件到服务器
android上传文件到服务器 重点:最好是设置好content-type这些参数的配置! package com.spring.sky.image.upload.network; ...
- android 上传文件
android对于上传文件,还是非常easy的,和java里面的上传都是一样的,基本上都是熟悉操作输出流和输入流!另一个特别重要的就是须要一些content-type这些參数的配置! 假设这些都弄好 ...
- C# 上传文件至远程服务器
C# 上传文件至远程服务器(适用于桌面程序及web程序) 2009-12-30 19:21:28| 分类: C#|举报|字号 订阅 最近几天在玩桌面程序,在这里跟大家共享下如何将本地文件上传 ...
- ASP.NET上传文件到远程服务器(HttpWebRequest)
/// <summary> /// 文件上传至远程服务器 /// </summary> /// <param name="url">远程服务地址 ...
- asp.net 服务器 上传文件到 FTP服务器
private string ftpServerIP = "服务器ip";//服务器ip private string ftpUserID = "ftp的用户名" ...
- 在C#客户端用HTTP上传文件到Java服务器
在C#客户端用HTTP上传文件到Java服务器 来源:http://www.cnblogs.com/AndyDai/p/5135294.html 最近在做C / S 开发,需要在C#客户端上传文件到 ...
- .Net 上传文件到ftp服务器和下载文件
突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...
随机推荐
- shell中cut用法
cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的. (1)其语法格式为:cut [-bn] [file] 或 cut ...
- Python安装指南
说明:我的安装环境是centos6.4 ,32位系统:(#号之后内容为注释说明内容) 1.准备 centos是自带python的,所以可以在shell下直接执行:python 可以看到相应的打印信息, ...
- 自定义ViewPager控制是否滑动
package com.lvshandian.menshen.view;/** * Created by zhang on 2016/11/8. * 创建自定义滑动,禁止滑动的ViewPager */ ...
- iOS开发debug跟release版本屏蔽NSLog方法
1.在***-Prefix.pch里面添加 #ifndef __OPTIMIZE__ # define NSLog(...) NSLog(__VA_ARGS__) #else # define NSL ...
- DLL输入和输出函数—dllinport与dllexport
Microsoft特殊处 dllimport和dllexport存储类修饰符是C语言的Microsoft特殊处扩充.这些修饰显式定义了DLL的客户界面(可执行的文件或另外的DLL).说明为dllexp ...
- which、whereis、locate、find 命令用法
which.whereis.locate.find 命令用法 大部分转自http://312788172.iteye.com/blog/730280,有修改 我们经常在linux要查找某个文件,但 ...
- 在服务器上log4net没写日志
登录到服务器上,发现log4net没写日志 在相应文件夹加上User用户的写权限后恢复正常了.
- [SAP ABAP开发技术总结]文本文件、Excel文件上传下传
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- mysql密码忘记或者不知道,怎么办?
运行cmd: 输入mysql回车,如果成功,将出现MySQL提示符 > 连接权限数据库>use mysql; (>是本来就有的提示符,别忘了最后的分号) 修改改密码:> upd ...
- hdu 4165 Pills dp
Pills Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem De ...