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如何上传文件,然 ...
随机推荐
- java安全令牌生成器
SecureRandom sr = new SecureRandom(); byte[] bytes = new byte[8]; bytes = sr.generateSeed(8); System ...
- tarjan算法模板
终于能自己完整的打下来 #include<cstdio> #include<cstring> #include<iostream> #include<vect ...
- 思考之一——PM(Project Manager)
摘自:http://blog.csdn.net/moreevan/article/details/6697806
- ord函数-php
摘录自http://php.net/manual/zh/function.ord.php (PHP 4, PHP 5, PHP 7) ord — 返回字符的 ASCII 码值 说明 int ord ( ...
- PCL点云库:对点云进行变换(Using a matrix to transform a point cloud)
点云数据可以用ASCII码的形式存储在PCD文件中(关于该格式的描述可以参考链接:The PCD (Point Cloud Data) file format).为了生成三维点云数据,在excel中用 ...
- 命令行运行R语言脚本(代码)
1 Windows: 键入 cd C:\Program Files\R\R-3.2.0\bin 工作目录切换到R的核心程序目录 键入 R BATCH F:\Test.R 或 Rscript F:\ ...
- JUnit 单元测试 配置
选中工程,右键 built path , add liberaries , JUnit , JUnit4 这样就不用每次测试时都在main方法中写了
- [JAVA设计模式]第二部分:创建模式
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- javascript权威指南笔记--javascript语言核心(六)
通过ECMAScript 3创建的属性都是可写的.可枚举的.可配置的. 在ECMAScript 5中,数据属性的4个特性分别是它的值.可写性.可枚举性.可配置性.存取器属性的特性是读取.写入.可枚举性 ...
- CSS深入研究:display的恐怖故事解密(2) - table-cell(转)
http://www.cnblogs.com/StormSpirit/archive/2012/10/24/2736453.html 上集<CSS深入研究:display的恐怖故事解密(1) - ...