一、使用struts来搭建一个简单的服务器
1.首先去struts的官网下载struts-2.3.32-all.zip包(本人小白,只用过这个),然后解压,需要用到此解压包中的三个个文件,所需文件的根目录如下图所示:
-
首先看下我们所需的文件的根目录:
-
一个xml配置文件(stuts.xml)如图所示:
-
一个目录下的jar包,如图所示:
2.下面介绍怎么使用,我搭建tomcat的IDE为Eclipse,具体步骤如下:
-
使用eclipse创建一个java项目,File-->new-->Dynamic Web Project,目录结构如下:
3.修改上述拷贝进去的web.xml文件和struts.xml文件
- web.xml修改如下:
配置代码如下:
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
- struts.xml修改如下:
3.具体的OkHttpServer类,参考代码如下:
package com.serenity.okhttpserver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 项目名称:OkHttpServers
* 类名称:OkHttpServer
* @author wanglu 2017年3月22日 下午2:18:22
* function:提供简单返回接口
*/
public class OkHttpServer extends ActionSupport{
private String username ;
private String userage ;
private String caseDetails = "{\"rescode\":\"200\",\"resdes\":\"案件详情获取成功\",\"tasknum\":\"1101201704101550417360821\",\"accidenttime\":\"2017-04-10 15:56:18\",\"address\":\"北京市朝阳区东柏街9号院-6号楼\",\"situation\":\"追尾\",\"description\":\"追尾的追尾的测试\",\"accidentimagelist\":[{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_20171510155622726.jpg\",\"imagetypename\":\"侧前方\",\"imagetype\":\"0\",\"imagedate\":\"2017-04-10 15:56:22\"},{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_20171510155625326.jpg\",\"imagetypename\":\"侧后方\",\"imagetype\":\"1\",\"imagedate\":\"2017-04-10 15:56:25\"},{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_20171510155627913.jpg\",\"imagetypename\":\"碰撞部位\",\"imagetype\":\"2\",\"imagedate\":\"2017-04-10 15:56:28\"},{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_20171510155630381.jpg\",\"imagetypename\":\"其它现场照片\",\"imagetype\":\"5\",\"imagedate\":\"2017-04-10 15:56:30\"},{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_2017151015563430.jpg\",\"imagetypename\":\"其它现场照片\",\"imagetype\":\"5\",\"imagedate\":\"2017-04-10 15:56:34\"},{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_20171510155637806.jpg\",\"imagetypename\":\"其它现场照片\",\"imagetype\":\"5\",\"imagedate\":\"2017-04-10 15:56:37\"},{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_20171510155654270.jpg\",\"imagetypename\":\"其它现场照片\",\"imagetype\":\"5\",\"imagedate\":\"2017-04-10 15:56:54\"},{\"imageaddress\":\"北京市朝阳区百子湾南二路88\",\"imageurl\":\"http://image.zhongchebaolian.com:80/photobase/photos/photo/2017-04-10/1101201704101550417360821/1101201704101550417360821_20171510155654270.jpg\",\"imagetypename\":\"其它现场照片\",\"imagetype\":\"5\",\"imagedate\":\"2017-04-10 15:56:54\"}],\"casevideolist\":[{\"videoaddress\":\"北京市朝阳区百子湾南二路88\",\"videourl\":\"http://106.2.184.230/image.zhongchebaolian.com/photobase/photos/video/2017-04-10/1101201704101550417360821/1101201704101550417360821_recording1491811000998.mp4\",\"videodate\":\"2017-04-10 15:56:47\"},{\"videoaddress\":\"北京市朝阳区百子湾南二路88\",\"videourl\":\"http://image.zhongchebaolian.com/photobase/photos/video/2017-04-10/1101201704101550417360821/1101201704101550417360821_recording1491811022494.mp4\",\"videodate\":\"2017-04-10 15:57:07\"},{\"videoaddress\":\"北京市朝阳区百子湾南二路88\",\"videourl\":\"http://image.zhongchebaolian.com/photobase/photos/video/2017-04-10/1101201704101550417360821/1101201704101550417360821_recording1491811033900.mp4\",\"videodate\":\"2017-04-10 15:57:23\"}],\"casecarlist\":[{\"casecarno\":\"津123488\",\"carownname\":\"测试甲\",\"carownphone\":\"15313922537\",\"driverno\":\"321123456654789987\",\"duty\":\"\"}]}";
public String requestCaseDetails() throws IOException{
System.out.println("username:"+username);
System.out.println("userage:"+userage);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write(caseDetails);
// writer.flush();
// writer.close();
return null ;
}
/**
* 读取客户端提交的json字符串
* @return
* @throws IOException
*/
public String postString() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
String id = request.getSession().getId();
System.out.println("-----sessionId------>"+id);
ServletInputStream inputStream = request.getInputStream();
StringBuilder sb = new StringBuilder();
int length = 0 ;
byte[] buf = new byte[1024];
while ((length=(inputStream.read(buf)))!=-1) {
sb.append(new String(buf, 0,length));
}
inputStream.close();
System.out.println("postString:"+sb.toString());
return null ;
}
/**
* post提交File
* @return
* @throws IOException
*/
public String postFile() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
ServletInputStream inputStream = request.getInputStream();
String id = request.getSession().getId();
System.out.println("-----sessionId------>"+id);
String dir = ServletActionContext.getServletContext().getRealPath("files");
File ff = new File(dir);
if(!ff.exists()){
ff.mkdirs();
}
File file = new File(dir, "animal.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(file);
int length = 0 ;
byte[] buf = new byte[1024];
while ((length = inputStream.read(buf))!=-1) {
fileOutputStream.write(buf, 0, length);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
return null ;
}
/**
* post上传文件
* @return
* @throws IOException
*/
public File mPhoto ;
public String mPhotoFileName ;
public String upLoadFile() throws IOException{
System.out.println("username:"+username);
System.out.println("userage:"+userage);
if(null==mPhoto){
System.out.println(mPhotoFileName +" is null .");
}
String dir = ServletActionContext.getServletContext().getRealPath("files");
File file = new File(dir, "wanglu.jpg");
FileUtils.copyFile(mPhoto, file);
// ServletInputStream inputStream = ServletActionContext.getRequest().getInputStream();
// FileOutputStream fileOutputStream = new FileOutputStream(file);
// int len = 0 ;
// byte[] buf = new byte[128];
// while ((len = inputStream.read(buf))!=-1) {
// fileOutputStream.write(buf, 0, len);
// }
// fileOutputStream.flush();
// fileOutputStream.close();
// inputStream.close();
return null ;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserage() {
return userage;
}
public void setUserage(String userage) {
this.userage = userage;
}
}
备注:请求完整路径如下:http://localhost:8080/项目名/struts.xml中的action的name值