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服务器的更多相关文章

  1. Android 上传文件到 FTP 服务器

    实现背景 近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然之前接触过FTP服务器,了解基本的使用流程,但是将此流程从使用习惯转化为代码实现还是有一定难度的.但是基本的流程还是 ...

  2. android上传文件到服务器

    package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...

  3. android -上传文件到服务器

    android上传文件到服务器       重点:最好是设置好content-type这些参数的配置!     package com.spring.sky.image.upload.network; ...

  4. android 上传文件

    android对于上传文件,还是非常easy的,和java里面的上传都是一样的,基本上都是熟悉操作输出流和输入流!另一个特别重要的就是须要一些content-type这些參数的配置!  假设这些都弄好 ...

  5. C# 上传文件至远程服务器

    C# 上传文件至远程服务器(适用于桌面程序及web程序) 2009-12-30 19:21:28|  分类: C#|举报|字号 订阅     最近几天在玩桌面程序,在这里跟大家共享下如何将本地文件上传 ...

  6. ASP.NET上传文件到远程服务器(HttpWebRequest)

    /// <summary> /// 文件上传至远程服务器 /// </summary> /// <param name="url">远程服务地址 ...

  7. asp.net 服务器 上传文件到 FTP服务器

    private string ftpServerIP = "服务器ip";//服务器ip private string ftpUserID = "ftp的用户名" ...

  8. 在C#客户端用HTTP上传文件到Java服务器

    在C#客户端用HTTP上传文件到Java服务器  来源:http://www.cnblogs.com/AndyDai/p/5135294.html 最近在做C / S 开发,需要在C#客户端上传文件到 ...

  9. .Net 上传文件到ftp服务器和下载文件

    突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...

随机推荐

  1. 2016年10月24日 星期一 --出埃及记 Exodus 19:8

    2016年10月24日 星期一 --出埃及记 Exodus 19:8 The people all responded together, "We will do everything th ...

  2. 转 Android学习笔记: 学习过程中碰到的一些问题及解决方法

    在学习Android开发的过程中遇到了不少的问题,所幸的是最终经过上网查询都得到了解决.现在将我在学习Android开发过程中遇到的一些问题及解决的方法整理如下. 1.R.java不能实时更新 问题描 ...

  3. 复旦大学2014--2015学年第一学期高等代数I期末考试情况分析

    一.期末考试成绩班级前几名 金羽佳(92).包振航(91).陈品翰(91).孙浩然(90).李卓凡(85).张钧瑞(84).郭昱君(84).董麒麟(84).张诚纯(84).叶瑜(84) 二.总成绩计算 ...

  4. CentOS 6.5中linux grub修复

    在使用Linux的过程中,难免会出现开机提示grub >而无法启动,可能是系统中/boot/grub文件丢失等原因造成的,当出现此问题的时候只要系统分区没有格式化一般是可以修复的,下面就以虚拟 ...

  5. MSM8909+Android5.1.1之系统烧录

    1.     安装高通USB驱动 图1 安装成功后,同时按下设备的音量+和音量-按键,且用USB连接到设备上,在设"设备管理器--->端口"下面看到USB虚拟的端口,如下: ...

  6. .Net面試題

    初级.NET开发人员 - 任何使用.NET的人都应知道的 1. 描述线程与进程的区别? 进程是系统所有资源分配时候的一个基本单位,拥有一个完整的虚拟空间地址,并不依赖线程而独立存在.进程可以定义程序的 ...

  7. [POJ2182]Lost Cows(树状数组,二分)

    题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...

  8. 通过数据库和EasyUI的combobox级联实现省市区三级联动

    1.新建一个web项目 2.因为这里用到了数据库所以我们在lib目录导入Hibernate的jar包.fastjson.jar包及数据库jar包 3.同样导入EasyUI的组件配置,并在新建的html ...

  9. [SAP ABAP开发技术总结]OPEN SQL

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. Codeforces Round #257 (Div. 2) B

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...