由于项目需要用到图片上传,所以Google,百度了半天发现大多数人的实现方法都是客户端用HttpUrlConnection通过HTTP协议实现上传,服务器则用两个Apache的类(具体忘了。。)接收,这种方法对我这种不懂协议的菜鸟来说有点麻烦,最后又查到了用客户端HttpClient发送服务器则可以直接读输入流也可以用上一种方法的做法接收。我采用了第二种,把参数写在url里面(像get方法一样),然后用MultipartEntity这个类(需要其他的jar包)上传文件。具体看代码:
客户端:
package com.alpha.yafoot;
import java.io.File;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity {
private Button test;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
test = (Button)findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
send();
}
});
}
private void send(){
String url = "http://192.168.23.1:8080/accompany/imgUpLoader?w=100&h=100";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpContext httpContext = new BasicHttpContext();
HttpPost post = new HttpPost(url);
try {
//生成本来的文件名
String path = Environment.getExternalStorageDirectory().getPath() + "/YaFoot/zxd.png";
File file = new File(path);
//添加内容
entity.addPart("file", new FileBody(file));
//把要传输的内容放到请求里面
post.setEntity(entity);
//创建客户端对象
HttpClient client = new DefaultHttpClient();
//执行请求并得到返回结果 服务器地址
HttpResponse response = client.execute(post, httpContext);
if(response.getStatusLine().getStatusCode() == 200){
String str = EntityUtils.toString(response.getEntity());
Log.i("dd", "Yes:--" + str);
}else {
Log.i("dd", "error");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务器:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class imgUpLoader extends HttpServlet {
//重写的doPost方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String width = request.getParameter("w");
String height = request.getParameter("h");
System.out.println(width + " " + height);
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
String serverRealPath = application.getRealPath("/") ;
response.setCharacterEncoding("UTF-8");
try {
InputStream is = request.getInputStream();
OutputStream os = new FileOutputStream("D:/zxd.png");
byte [] buffer = new byte [1024];
int length = -1;
//以MultipartEntity的方式接收上传文件,流中会包含描述信息,
//去掉第一次读取的内容,即可除去,保证只接收文件内容
boolean first = true;
while ((length = is.read(buffer)) != -1) {
if (!first) {
os.write(buffer, 0, length);
}
first = false;
}
os.flush();
is.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
response.getWriter().write("recv");
}
}
主要参考资料:
http://www.eoeandroid.com/thread-76721-1-1.html
http://my.eoe.cn/yimei/archive/5087.html
jar包下载: