Android上传文件到Web服务器

php – file upload

通常采用构造http协议的方法,模拟网页POST方法传输文件,服务器端可以采用JavaServlet(较为常见)或者PHP来 接收要传输的文件。A very useful aspect of PHP is its ability to manage file uploads to your server. Allowing users to upload a file to your server opens a whole can of worms.

手机客户端:使用标准http协议实现,(HttpUrlConneciton)

1. get请求

URL url=new URL(“http://www.baidu.com/receiver.php”);//服务器上接收文件的处理页面,换成自己的url
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
is.read(…..

 2. post请求

“POST” – Informs the browser that we want to send information to the server using POST.

将参数用 & 和 = 连接,然后用流写出去。

URL url = new URL("http://localhost/upload/upload.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);//允许输入流
conn.setDoOutput(true);//允许输出流
conn.setUseCaches(false); //不允许使用缓存
conn.setRequestMethod("POST"); //请求方式
conn.setRequestProperty("Charset", CHARSET); //设置编码
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);

if(file!=null)
{
/**
* 当文件不为空,把文件包装并且上传
*/
DataOutputStream dos = new DataOutputStream( conn.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
OutputStream out=conn.getOutputStream();
out.write(“wd=abcdefg&wd2=hijklmn”.getBytes());
out.flush();
out.close();
InputStream is = conn.getInputStream();
is.read(…..

一个文本文件 test.txt 内容为

content of test.txt
上传时浏览器发送的格式是
-----------------------------7d65d38307d2//分隔符 
Content-Disposition: form-data; name="myfile"; filename="E:\badboy\test.txt"//表单名是myfile, 文件是E:\badboy\test.txt 
Content-Type: text/plain content of test.txt//文件的内容 
-----------------------------7d65d38307d2--//分隔符 

在Java中定义数据分隔线 
String BOUNDARY = "------------------------7dc2fd5c0894"; 
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线
StringBuilder sb = new StringBuilder();
                sb.append("--");
                sb.append(BOUNDARY);
                sb.append("\r\n");
                sb.append("Content-Disposition: form-data;name=\"img[]\";filename=\""
                        + file.getName() + "\"\r\n");
                sb.append("Content-Type:image/pjpeg\r\n\r\n");
                out.write(sb.toString().getBytes());

                DataInputStream in = new DataInputStream(new FileInputStream(file));
                int bytes = 0;
                byte[] bufferOut = new byte[1024];
                while ((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }
                out.write("\r\n".getBytes());
                in.close();
            }
            out.write(end_data);
            out.flush();
            out.close();

2. PHP服务端
PHP file should make a key decision with all uploads: keep the file or throw it away; To save our precious file we are going to need to make use of the $_FILES,The $_FILES array is where PHP stores all the information about files.

uploadedfileuploadedfile is the reference when we post a file from a client (we assigned in our HTML form). We will need this to tell the $_FILES array which file we want to play around with.
$_FILES[‘uploadedfile’][‘name‘] name contains the original path of the user uploaded file.
$_FILES[‘uploadedfile’][‘tmp_name‘]tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: