您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
44_采用httpclient上传数据
发布时间:2021-02-26 11:00:14编辑:雪饮阅读()
搭建一个复合型post接口(普通表单数据以及文件数据)
首先在之前的javaweb项目中建立一个jsp表单(复合表单)如Login.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="/javaweb/LoginServlet" method="post">
<p><label>用户名:</label><input name="name" value="" /></p>
<p><label>密码:</label><input name="password" value="" /></p>
<p><input type="submit" value="提交" /></p>
</form>
</body>
</html>
接下来要实现这个表单对应提交接口post的具体方法,由于包含文件上传,需要用到如下几个类:
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
而这几个类一般情况下在eclipse中无法导入,不自带,至少我这个eclipse不自带
Eclipse Java EE IDE for Web Developers.
Version: Indigo Service Release 2
Build id: 20120216-1857
(c) Copyright Eclipse contributors and others 2005, 2012. All rights reserved.
据说要依赖下面这两个jar包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
不过我感觉commons-io-2.0.1.jar包应该作用不大,只是为了谨慎起见,所以都加进来了。
那么如何将这种第三方jar包添加进eclipse中呢,方法因具体情况而定。在我这个eclipse中并且在这个Javaweb项目中只需要将这两个jar包拷贝到WebContent/ WEB-INF/ lib目录中即可
然后之前的LoginServlet.java中对post接口整改,整合为现在最新的包含普通表单post和文件上传于一体的功能的具体实现如:
package web;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
publicclass LoginServlet extends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
if(name!=null){
name=new String(name.getBytes("iso8859-1"),"utf-8");
}
String password=request.getParameter("password");
System.out.println(name);
System.out.println(password);
if("zhangsan".equals(name) && "123456".equals(password)){
response.getOutputStream().write("login succes".getBytes());
}
else{
response.getOutputStream().write("login failed".getBytes());
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//判断enctype属性是否为multipart/form-data
if(isMultipart){
String realpath = request.getSession().getServletContext().getRealPath("/files");
System.out.println(realpath);
File dir = new File(realpath);
if(!dir.exists()) dir.mkdirs();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
try {
List<FileItem> items = upload.parseRequest(request);
for(FileItem item : items){
if(item.isFormField()){
String name1 = item.getFieldName();
String value = item.getString("UTF-8");
System.out.println(name1+ "="+ value);
}else{
String fileType=item.getName().substring(item.getName().lastIndexOf("."));
item.write(new File(dir, System.currentTimeMillis()+fileType ));
}
}
System.out.println("你好");
response.getOutputStream().write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}else{
doGet(request, response);
}
}
}
然后将上面新建的Login.jsp和整合最新post接口需求的LoginServlet.java都重新部署到服务进行web端提交测试。
从上面这个图的控制台中可以看到第一行的字符串显示的就是上传之后的文件所存储的路径,那么进入这个路径一探究竟
看来这个新的复合型post数据提交接口就这样完成了。
复合型post接口的对接
复合型post接口完成后,就需要在安卓上进行对接,对于安卓端对接同样也有可能需要加载第三方jar包,这里所需要的jar有如下3个:
commons-codec-1.3.jar、commons-httpclient-3.1.jar、commons-logging-1.1.jar
那么在安卓上的android studio中要加载这3个jar包,可以按如下步骤进行添加。
首先将这3个jar包放到一个目录
然后在android studio中添加jar依赖
这里步骤1选择刚才存放3个jar包的目录,步骤2这里就用默认这个
最后应用下就可以了。
这里需要注意下这3个依赖因环境而异,有的环境则因为commons-codec-1.3.jar、commons-logging-1.1.jar两个而导致类冲突问题,则如果以及添加了这两个,则直接再目录中删除这两个jar即可,然后你可能需要进入这个界面进行确认下,如果只存在一个commons-httpclient-3.1.jar包则就ok了。最好clean project 然后重新rebuild project。为了谨慎起见嘛,最后再将项目部署到设备。
package com.example.login;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.commons.httpclient.methods.multipart.Part;
public class DataService {
public static String sendDataByGet(String path, String name, String password)
throws Exception {
String param1 = URLEncoder.encode(name);
String param2 = URLEncoder.encode(password);
URL url = new URL(path + "?name=" + param1 + "&password=" + param2);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
InputStream is = conn.getInputStream();
byte[] result = StreamTool.getBytes(is);
return new String(result);
}
public static String sendDataByPost(String path, String name,
String password) throws Exception {
String param1 = URLEncoder.encode(name);
String param2 = URLEncoder.encode(password);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String data = "name=" + param1 + "&password=" + param2;
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
// 设置 http协议可以向服务器写数据
conn.setDoOutput(true);
// 设置http协议的消息头
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length() + "");
// 把我们准备好的data数据写给服务器
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
// httpurlconnection 底层实现 outputstream 是一个缓冲输出流
// 只要我们获取任何一个服务器返回的信息 , 数据就会被提交给服务器 , 得到服务器返回的流信息
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
byte[] result = StreamTool.getBytes(is);
return new String(result);
} else {
throw new IllegalStateException("服务器状态异常");
}
}
public static String sendDataByHttpClientGet (String path , String name,String password) throws Exception{
//1. 获取到一个浏览器的实例
HttpClient client = new DefaultHttpClient();
//2. 准备请求的地址
String param1 = URLEncoder.encode(name);
String param2 = URLEncoder.encode(password);
HttpGet httpGet = new HttpGet(path + "?name=" + param1 + "&password=" + param2);
//3.发请求
HttpResponse ressponse = client.execute(httpGet);
int code = ressponse.getStatusLine().getStatusCode();
if(code == 200){
InputStream is =ressponse.getEntity().getContent();
byte[] result = StreamTool.getBytes(is);
return new String(result);
}
else{
throw new IllegalStateException("服务器状态异常");
}
}
public static String sendDataByHttpClientPost(String path , String name,String password) throws Exception{
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(path);
// 键值对
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("name", name));
parameters.add(new BasicNameValuePair("password", password));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
//3.设置post请求的数据实体
httppost.setEntity(entity);
//4. 发送数据给服务器
HttpResponse ressponse = client.execute(httppost);
int code = ressponse.getStatusLine().getStatusCode();
if(code == 200){
InputStream is =ressponse.getEntity().getContent();
byte[] result = StreamTool.getBytes(is);
return new String(result);
}
else{
throw new IllegalStateException("服务器状态异常");
}
}
/**
* 提交数据给服务器 带一个文件
* @param path
* @param name
* @param password
* @param filepath 文件在手机上的路径
*
* @return
* @throws Exception
*/
public static String sendDataByHttpClientPost(String path , String name,String password ,String filepath) throws Exception{
// 实例化上传数据的 数组 part []
Part[] parts = {new StringPart("name", name),
new StringPart("password", password),
new FilePart("file", new File(filepath))};
PostMethod filePost = new PostMethod(path);
//模拟html表单的multipart/form-data
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if(status==200){
System.out.println( "服务端返回编码:"+filePost.getResponseCharSet());
///乱码处理
InputStream in = filePost.getResponseBodyAsStream();
//这里使用8859-1读取
BufferedReader br = new BufferedReader(new InputStreamReader(in,"ISO-8859-1"));
String tempbf;
StringBuffer html = new StringBuffer(100);
while ((tempbf = br.readLine()) != null) {
html.append(tempbf +"\n");
}
//将8859-1再次转成GB2312
String result_decode=new String(html.toString().getBytes("ISO-8859-1"),"GB2312");
System.out.println("result_decode:"+result_decode);
return result_decode;
}
else{
throw new IllegalStateException("服务器状态异常");
}
}
}
然后新增调用这个复合post接口的按钮、及文件路径输入框(文件路径从设备的存储路径中手动拼接,就不做成选择文件的方式了)的元素值strings.xml:
<resources>
<string name="app_name">Login</string>
<string name="input_name">请输入用户名</string>
<string name="input_password">请输入密码</string>
<string name="get_login">采用get方式登陆</string>
<string name="post_login">采用post方式登陆</string>
<string name="client_get_login">httpclient的get登陆</string>
<string name="client_post_login">httpclient的post登陆</string>
<string name="input_filepath">请输入上传的文件路径</string>
<string name="client_post_login_with_file">提交带文件的数据</string>
</resources>
然后主程序对应事件处理MainActivity.java:
package com.example.login;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtName;
private EditText mEtPassword;
private Button mBtLogin;
private Button mBtLoginPost;
private Button mBtLoginClientGet;
private Button mBtLoginClientPost;
private EditText mEtFilePath;
private Button mBtLoginClientPostFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtName = this.findViewById(R.id.et_name);
mEtPassword = this.findViewById(R.id.et_password);
mBtLogin = this.findViewById(R.id.bt_login);
mBtLogin.setOnClickListener(this);
mBtLoginPost = this.findViewById(R.id.bt_login_post);
mBtLoginPost.setOnClickListener(this);
mBtLoginClientGet = this.findViewById(R.id.bt_login_client_get);
mBtLoginClientGet.setOnClickListener(this);
mBtLoginClientPost = this.findViewById(R.id.bt_login_client_post);
mBtLoginClientPost.setOnClickListener(this);
mEtFilePath = this.findViewById(R.id.et_file_path);
mBtLoginClientPostFile = this.findViewById(R.id.bt_login_client_post_file);
mBtLoginClientPostFile.setOnClickListener(this);
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
String name = mEtName.getText().toString().trim();
String password = mEtPassword.getText().toString().trim();
if("".equals(name)||"".equals(password)){
Toast.makeText(this, "用户名或密码不能为空", 0).show();
return;
}
String path = getResources().getString(R.string.servleturl);
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
switch (v.getId()) {
case R.id.bt_login:
// 通过get请求 发送数据到服务器
try {
String result = DataService.sendDataByGet(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_post:
try {
String result = DataService.sendDataByPost(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_get:
try {
String result = DataService.sendDataByHttpClientGet(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_post:
try {
String result = DataService.sendDataByHttpClientPost(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_post_file:
try {
String filepath = mEtFilePath.getText().toString().trim();
if("".equals(filepath)){
Toast.makeText(this, "路径不能为空", 0).show();
return ;
}
String result = DataService.sendDataByHttpClientPost(path, name, password, filepath);
Toast.makeText(this, result, 0).show();
System.out.println(result);
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
}
}
}
因为要读取设备中的存储路径,则需要存储的读取权限,清单文件上也要加上AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.login">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Login"
android:usesCleartextTraffic="true"
>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最后就是布局文件了activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/input_name" />
<EditText
android:id="@+id/et_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/input_password" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/get_login"
android:id="@+id/bt_login"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/post_login"
android:id="@+id/bt_login_post"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/client_get_login"
android:id="@+id/bt_login_client_get"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/client_post_login"
android:id="@+id/bt_login_client_post"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/et_file_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="/mnt/sdcard/1.png"
android:hint="@string/input_filepath" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/client_post_login_with_file"
android:id="@+id/bt_login_client_post_file"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
然后项目部署到设备中并在设备中同意清单文件中的存储权限勾选,就可以进行测试了
要上传的是设备中的这个图
然后用户名和密码随便填一些内容,不为空就可以,尽量稍微正式些。。。
这边测试通过,同时查看到javaweb项目LoginServlet.java运行在服务时对应的控制台的信息,也是正常输出的。
最后查看下文件是否上传成功,根据输出的目录信息,进入对应目录确认文件的确是上传成功的,那么这个post复合类型的接口就对接成功了
关键字词:android,httpclient,上传