最近在做一个android名片管理系统的demo,主要功能是与服务器端通信上传及下载数据。
android访问服务器端下载基本没什么问题,但是android上传到服务器遇到瓶颈。android上传文件以数据流的形式上传,所以采用模拟http请求将文件以数据流的形式上传到服务器端,代码如下:
import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class mainActivity extends Activity { /* * 变量声明 filename:上传后在服务器上的文件名称 uploadFile:要上传的文件路径 actionUrl:服务器上对应的程序路径 */ private String srcPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"download"+ File.separator+"tilocal.jpeg"; private String newName = "tilocal.jpeg"; //要上传的本地文件路径 private String uploadFile = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"download"+ File.separator+"tilocal.jpeg"; //上传到服务器的指定位置(废弃,在uploadFile方法中写访问服务器路径) //private String actionUrl = "http://192.168.3.10:8080//android/customer/upLoad.action";
private TextView mTextView1;
private TextView mTextView2; private Button mButton1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView1 = (TextView) findViewById(R.id.mTextView1); mTextView1.setText("FilePath:/n " + uploadFile); mTextView2 = (TextView) findViewById(R.id.mTextView2); //mTextView2.setText("UploadPath:/n " + actionUrl); /* 设定mButton的onClick事件处理 */ mButton1 = (Button) findViewById(R.id.mButton1); mButton1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { uploadFile(); } }); }private void uploadFile() { String uploadUrl = "http://10.0.2.2:8080/upLoadFile/servlet/UpLoadFile"; String end = "\r\n"; String twoHyphens = "--"; String boundary = "******"; try { URL url = new URL(uploadUrl); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream()); dos.writeBytes(twoHyphens + boundary + end); dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + srcPath.substring(srcPath.lastIndexOf("/") + 1) + "\"" + end); dos.writeBytes(end); FileInputStream fis = new FileInputStream(srcPath); byte[] buffer = new byte[8192]; // 8k int count = 0; while ((count = fis.read(buffer)) != -1) { dos.write(buffer, 0, count); } Log.e("", dos.size()+"----------"); fis.close(); dos.writeBytes(end); dos.writeBytes(twoHyphens + boundary + twoHyphens + end); dos.flush(); InputStream is = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String result = br.readLine(); Toast.makeText(this, result, Toast.LENGTH_LONG).show(); dos.close(); is.close(); } catch (Exception e) { e.printStackTrace(); setTitle(e.getMessage()); } } }
以上代码为android模拟http协议上传文件,以数据流的形式上传。
下面为服务器端servlet接收代码:
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//获取输入流,是HTTP协议中的实体内容ServletInputStream in=request.getInputStream();//缓冲区byte buffer[]=new byte[1024];//写入服务器端固定路径磁盘中FileOutputStream out=new FileOutputStream("F:\\upload\\zzz.jpeg");int len=in.read(buffer, 0, 1024);//把流里的信息循环读入到file文件中System.out.println(len+"+++++++++++++");while( len!=-1 ){System.out.println(len+"----------");out.write(buffer, 0, len);len=in.readLine(buffer, 0, 1024);}out.close();in.close();}
以上代码为粗糙的实现了android客户端与服务器端文件的上传功能。