Struts2Utils方法详解,struts2utils详解

package com.apdplat.platform.util;   import
com.apdplat.platform.log.APDPlatLogger; import
java.io.IOException; import java.io.OutputStream; import
java.util.Collection; import java.util.Map; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import
org.apache.commons.lang.StringUtils; import
org.apache.struts2.ServletActionContext; /** *Struts2工具类
* @author 杨尚川 */ public class Struts2Utils {  
``protected static final APDPlatLogger log = ``new
APDPlatLogger(Struts2Utils.``class``); `        private`
`Struts2Utils(){`   `};`   `private` `static` `final`
`String ENCODING_PREFIX =
“encoding”;` `private` `static` `final`
`String NOCACHE_PREFIX =
“no-cache”;` `private` `static` `final`
`String ENCODING_DEFAULT =
“UTF-8”;` `private` `static` `final`
`boolean` `NOCACHE_DEFAULT =
true;`   `private` `static` `final`
`String TEXT_TYPE =
“text/plain”;` `private` `static` `final`
`String JSON_TYPE =
“application/json”;` `private` `static` `final`
`String XML_TYPE =
“text/xml”;` `private` `static` `final`
`String HTML_TYPE =
“text/html”;` `private` `static` `final`
`String JS_TYPE =
“text/javascript”;`   `public` `static`
`HttpSession getSession() {`   `return`
`ServletActionContext.getRequest().getSession();`   `}`   `public`
`static` `HttpServletRequest getRequest() {`   `return`
`ServletActionContext.getRequest();`   `}`   `public` `static`
`HttpServletResponse getResponse() {`   `return`
`ServletActionContext.getResponse();`   `}`   `public` `static`
`String getParameter(String name) {`   `return`
`getRequest().getParameter(name);`   `}`     `public` `static` `void`
`render(
final`String contentType, ``final String content, ``final
String... headers) {   try {   //分析headers参数
String encoding = ENCODING_DEFAULT; boolean
noCache = NOCACHE_DEFAULT; for (String header : headers) {  
String headerName = StringUtils.substringBefore(header, ``":"``);
String headerValue = StringUtils.substringAfter(header, ``":"``);  
if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {  
encoding = headerValue;   } ``else if
(StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {  
noCache = Boolean.parseBoolean(headerValue);   } ``else {  
``throw new
IllegalArgumentException(headerName + ``"不是一个合法的header类型"``);
`}`}  
HttpServletResponse response = ServletActionContext.getResponse();  
//设置headers参数
String fullContentType = contentType + ``";charset=" + encoding;
response.setContentType(fullContentType); if (noCache) {  
response.setHeader(``"Pragma"``, ``"No-cache"``);
response.setHeader(``"Cache-Control"``, ``"no-cache"``);
response.setDateHeader(``"Expires"``, ``0``);   }  
response.getWriter().write(content); response.getWriter().flush();  
  } ``catch (IOException e) {   log.error(e.getMessage(), e);  
}   }   /** ``* 直接输出文本.
``* @see #render(String, String, String...) ``*/ public static
void renderText(``final String text, ``final
String... headers) {   render(TEXT_TYPE, text, headers);   }  
/** ``* 直接输出HTML.
``* @see #render(String, String, String...) ``*/ public static
void renderHtml(``final String html, ``final
String... headers) {   render(HTML_TYPE, html, headers);   }  
/** ``* 直接输出XML. ``* @see #render(String, String, String...)
``*/ public static void renderXml(``final
String xml, ``final String... headers) {  
render(XML_TYPE, xml, headers);   }   /** ``* 直接输出JSON.
``* ``* @param jsonString json字符串.
``* @see #render(String, String, String...) ``*/ public static
void renderJson(``final String jsonString, ``final
String... headers) {   render(JSON_TYPE, jsonString, headers);   }
  /** ``* 直接输出JSON. ``*
``* @param map Map对象,将被转化为json字符串.
``* @see #render(String, String, String...) ``*/
@SuppressWarnings``(``"unchecked"``) public static void
renderJson(``final Map map, ``final String... headers) {  
String jsonString = JSONObject.fromObject(map).toString();
render(JSON_TYPE, jsonString, headers);   }   /**
``* 直接输出JSON. ``*
``* @param object Java对象,将被转化为json字符串.
``* @see #render(String, String, String...) ``*/ public static
void renderJson(``final Object object, ``final
String... headers) {  
String jsonString = JSONObject.fromObject(object).toString();
render(JSON_TYPE, jsonString, headers);   }   /**
``* 直接输出JSON. ``*
``* @param collection Java对象集合, 将被转化为json字符串.
``* @see #render(String, String, String...) ``*/ public static
void renderJson(``final Collection<?> collection, ``final
String... headers) {  
String jsonString = JSONArray.fromObject(collection).toString();
render(JSON_TYPE, jsonString, headers);   }   /**
``* 直接输出JSON. ``*
``* @param array Java对象数组, 将被转化为json字符串.
``* @see #render(String, String, String...) ``*/ public static
void renderJson(``final Object[] array, ``final
String... headers) {  
String jsonString = JSONArray.fromObject(array).toString();
render(JSON_TYPE, jsonString, headers);   } ``public
static void renderImage(``byte``[] data,String type){  
``try {  
``HttpServletResponse response = ServletActionContext.getResponse();
``response.setContentType(type);
``try
(OutputStream out = response.getOutputStream()) {  
``out.write(data, ``0``, data.length);
``out.flush(); `}`  } ``catch (IOException e) {  
``log.error(``"渲染图像失败"``,e); `}`  }   /** ``* 直接输出支持跨域Mashup的JSONP. ``*
``* @param callbackName callback函数名.
``* @param contentMap Map对象,将被转化为json字符串.
``* @see #render(String, String, String...) ``*/
@SuppressWarnings``(``"unchecked"``) public static void
renderJsonp(``final String callbackName, ``final
Map contentMap, ``final String... headers) {  
String jsonParam = JSONObject.fromObject(contentMap).toString();  
StringBuilder result = ``new
StringBuilder().append(callbackName).append(``"("``).append(jsonParam).append(``");"``);
 
//渲染Content-Type为javascript的返回内容,输出结果为javascript语句, 如callback197("{
content:``'Hello World!!!' }");
render(JS_TYPE, result.toString(), headers);   }   }

package
com.apdplat.platform.util; import
com.apdplat.platform.log.APDPlatLogger; import java.io.IOException;
import java.io.OutputStr…

android中服务器到底怎么才连得上啊!!!
写了好久,服务器就是连不上,求大神帮忙看看。
package com.ll.server;

未赋值的session在jsp报错,求大神帮帮忙吧
想要在页面打开是判定一个session会话,这个session要等到跳转到服务器之后才能赋值。
但是明明我设定了判断是否为空,可是还是报错。
这是我的页面和服务器代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

<%@ page language=”java” import=”java.util.*”
pageEncoding=”UTF-8″%>

public class server extends Thread{
private Socket socket = null;

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

public server (Socket socket){}static HashMap map = new HashMap();static{//用户存储,使用map模拟数据库 map.put("liujiao", "123");}static HashMap pools = new HashMap();public static Set<String> set = new HashSet();public void run(){ try{ InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); byte[] b = new byte[100]; in.read; String cmd = new String; if(cmd.startsWith{//登录 //登录格式 username,password, String str[] = cmd.split;//按逗号分割 String username = str[1]; String password = str[2]; try{ if(map.get.equals){ out.write("ok".getBytes; out.flush(); set.add(socket.getInetAddress().getHostAddress;//set不存重复值 //set.add(socket.getLocalAddress().getHostAddress; return ; } throw new Exception(); }catch(Exception e){ out.write("error".getBytes; out.flush(); } }else if(cmd.startsWith{//下载 //下载格式 get,16565.amr try{ String filename = cmd.split[1]; File f = new File("D:\\amr", filename); if(f.exists{ out.write(("ok," + f.length.getBytes; out.flush(); byte[] b1 = new byte[10]; in.read; FileInputStream fin = new FileInputStream; int len = 0; byte[] b2 = new byte[1024]; while((len = fin.read != -1){ out.write; out.flush(); } fin.close(); return ; } throw new Exception(); }catch(Exception e){ out.write("error".getBytes; out.flush(); } }else if(cmd.startsWith){//上传 //上传格式 upload,15644414,username String s = cmd.split[1]; String username = cmd.split[2]; long size = Long.parseLong; out.write("ok".getBytes; out.flush(); File ff = new File("D:\\amr", (new Date().getTime + "R"+  (Math.random) + ".amr"); FileOutputStream fout = new FileOutputStream; byte[] b1 = new byte[1024]; int len = 0; long length = 0; while((len=in.read != -1){ length+=len; fout.write; if(length >= size){ break; } } fout.close(); Pool p = new Pool(); pools.put; p.message = username + "," + ff.getName(); for (String ip:set){ if(ip.equalsIgnoreCase(socket.getInetAddress().getHostAddress //if(ip.equalsIgnoreCase(socket.getLocalAddress().getHostAddress continue; p.ips.add; } p.start(); } }catch(Exception e){ }finally{ try{ socket.close(); }catch(Exception e1){ } }}public static void openServer() throws Exception{ ServerSocket server = new ServerSocket; while{ new server (server.accept.start(); System.out.println; }}
<title>请填写上传表单</title>

}

<%
Object o = session.getAttribute;
if(!o.equals||(!””.equals{
String er = o;

package com.ll.server;

%> 操作失败,<%=er %><%}

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

%>

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注