?? requestutil.java
字號:
package jaction.utility;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.ServletException;
import jaction.utility.SysLogger;
import jaction.utility.FileUtil;
import jaction.upload.*;
import jaction.workspace.JactionConfigResource;
/**
* HttpRequest 請求工具類<br>
* @author yanger
* @version 1.4.2.4
*/
public class RequestUtil extends Object {
/**
* 得到request中的所有參數(shù)
* @param request http請求接口
* @return map 參數(shù)map
*/
public static Map getAllParameters(HttpServletRequest request) {
Map bufferMap = Collections.synchronizedMap(new HashMap());
try{
for (Enumeration em = request.getParameterNames() ; em.hasMoreElements() ;) {
String name = (String)(em.nextElement());
String[] values = request.getParameterValues(name);
String[] temp = new String[values.length];
//chinese
for(int i=0;i<values.length;i++){
values[i] = StringUtil.CharSetConvert(values[i],StringUtil.ISO_8859_1,StringUtil.GBK);
temp[i]=values[i];
}
bufferMap.put(name,temp);
}
for (Enumeration em = request.getAttributeNames() ; em.hasMoreElements() ;) {
String name = (String)(em.nextElement());
Object object = request.getAttribute(name);
bufferMap.put(name,object);
}
}catch(Exception e){
SysLogger.error("RequestUtil","getAllParameters","Exception:"+e);
e.printStackTrace();
}
//upload init
//weigang 20030511 判斷是否是上傳文件
if(request.getContentLength() > 0){
multipartRequest(request,bufferMap);
}
return bufferMap;
}
/**
* 得到request的servletPath
* @param request http請求接口
* @return String servletName(沒有擴展名)
*/
public static String getServletNameNoExtend(HttpServletRequest request){
String servletPath = request.getServletPath();
int virguleIndex = servletPath.indexOf('/');
int dotIndex = servletPath.indexOf('.');
String servletName = "";
if(dotIndex==-1){
//servlet沒有擴展名
servletName = servletPath.substring(virguleIndex+1);
}else{
servletName = servletPath.substring(virguleIndex+1, dotIndex);
}
return servletName;
}
/**
* 得到request的servletPath
* @param request http請求接口
* @return String servletName(有擴展名)
*/
public static String getServletName(HttpServletRequest request){
String servletPath = request.getServletPath();
int virguleIndex = servletPath.indexOf('/');
String servletName = servletPath.substring(virguleIndex+1);
return servletName;
}
/**
* This method populates the internal hashtables with multipart request data.
* If the request argument is an instance of MultipartRequestWrapper,
* the request wrapper will be populated as well.
* @param request HttpServletRequest接口
* @param bufferMap 數(shù)據(jù)集
*/
public static void multipartRequest(HttpServletRequest request,Map bufferMap) {
try{
//deleteTempFiles
deleteTempFiles(getTempDir());
MultipartIterator iterator = new MultipartIterator(request,4096,getMaxSize(),getTempDir());
MultipartElement element;
while ((element = iterator.getNextElement()) != null) {
if (!element.isFile()) {
if (request instanceof MultipartRequestWrapper) {
((MultipartRequestWrapper) request).setParameter(element.getName(),
element.getValue());
}
String[] textValues = (String[]) bufferMap.get(element.getName());
if (textValues != null) {
String[] textValues2 = new String[textValues.length + 1];
System.arraycopy(textValues, 0, textValues2, 0, textValues.length);
textValues2[textValues.length] = element.getValue();
textValues = textValues2;
}else {
textValues = new String[1];
textValues[0] = element.getValue();
}
bufferMap.put(element.getName(), textValues);
bufferMap.put(element.getName(), textValues);
}else {
File tempFile = element.getFile();
if (tempFile.exists()) {
DiskFile theFile = new DiskFile(tempFile.getAbsolutePath());
theFile.setContentType(element.getContentType());
theFile.setFileName(element.getFileName());
theFile.setFileSize((int) tempFile.length());
bufferMap.put(element.getName(), theFile);
bufferMap.put(element.getName(), theFile);
}
}
}
}catch(Exception e){
e.printStackTrace();
SysLogger.error("RequestUtil","multipartRequest","Exception:"+e);
}
//
}
/**
* Gets the maximum post data size in bytes from the string
* representation in ActionServlet
*/
protected static long getMaxSize() throws ServletException{
String stringSize = JactionConfigResource.getMessage("jaction.upload.maxFileSize");
long size = -1;
int multiplier = 1;
if (stringSize.endsWith("K")) {
multiplier = 1024;
stringSize = stringSize.substring(0, stringSize.length()-1);
}
if (stringSize.endsWith("M")) {
multiplier = 1024*1024;
stringSize = stringSize.substring(0, stringSize.length()-1);
}
else if (stringSize.endsWith("G")) {
multiplier = 1024*1024*1024;
stringSize = stringSize.substring(0, stringSize.length()-1);
}
try {
size = Long.parseLong(stringSize);
}
catch (NumberFormatException nfe) {
throw new ServletException("Invalid format for maximum file size: \"" +
stringSize + "\"");
}
return (size * multiplier);
}
/**
* Gets the temp dir of upload
*/
protected static String getTempDir() throws ServletException{
return JactionConfigResource.getMessage("jaction.upload.tempdir");
}
/**
* Delete all the files uploaded
*/
public static void deleteTempFiles(String dir) {
File tempdir = new File(dir);
if(!tempdir.isDirectory())return;
File[] fileList = tempdir.listFiles();
for(int i=0;i<fileList.length;i++){
if(fileList[i].getName().endsWith(".tmp")){
if(fileList[i].exists())
fileList[i].delete();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -