zhdh2008 / JohnsonUtils4Droid

Useful l tools library for Android
7 stars 0 forks source link

如何搭建一个简易的App Server #92

Open zhdh2008 opened 6 years ago

zhdh2008 commented 6 years ago
  1. 首先实现一个简单的客户端,使用Eclipse创建一个Java Project,使用Okhttp3.10.0库实现客户端类,以下实现POST Form表单的形式实现文件上传功能 `package com.johnson4droid.client;

import java.io.File; import java.io.IOException;

import com.johnson4droid.client.log.LoggingInterceptor; import com.johnson4droid.client.log.LoggingLevel;

import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response;

/**

}`

zhdh2008 commented 6 years ago

日志 `package com.johnson4droid.client.log;

/**

zhdh2008 commented 6 years ago

`package com.johnson4droid.client.log;

import java.io.EOFException; import java.io.IOException; import java.nio.charset.Charset; import java.util.Locale; import java.util.concurrent.TimeUnit;

import okhttp3.Connection; import okhttp3.Headers; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import okhttp3.internal.http.HttpHeaders; import okhttp3.internal.platform.Platform; import okio.Buffer; import okio.BufferedSource;

/**

zhdh2008 commented 6 years ago
  1. 客户端实现完成,下面介绍App Server的实现 首先通过Eclipse J2EE新建一个Dynamic Web Project,然后新建一个ClientRequestHandler(继承自HttpServlet)来实现服务端,代码如下: `package com.johnson4droid.server;

import java.io.IOException;

import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part;

import com.alibaba.fastjson.JSONObject; import com.johnson4droid.server.utils.DateUtils; import com.johnson4droid.server.utils.IoUtils;

/**

}`

zhdh2008 commented 6 years ago

在doPost方法中,我们接收字符串参数以及文件参数,并将接收到的文件保存到磁盘中。 以下是两个工具类的代码 `package com.johnson4droid.server.utils;

import java.text.SimpleDateFormat; import java.util.Calendar;

/**

}`

zhdh2008 commented 6 years ago

`package com.johnson4droid.server.utils;

import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream;

/**

}`

zhdh2008 commented 6 years ago

最后贴一下web.xml中的配置 `

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

HttpServer
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>clientRequest</servlet-name>
    <servlet-class>com.johnson4droid.server.ClientRequestHandler</servlet-class>
    <multipart-config>
        <max-file-size>1000000</max-file-size>
    </multipart-config>
</servlet>

<servlet-mapping>
    <servlet-name>clientRequest</servlet-name>
    <url-pattern>/clientRequest</url-pattern>
</servlet-mapping>

`

zhdh2008 commented 6 years ago

最后将我们创建的服务端程序发布到Tomcat中即可完成服务端发布,可使用上面的客户端代码进行测试。