📄 httputils.java
字号:
// %1047610981968:com.neusoft.im2.util%
/* Auto record by SourceSafe
*****************************************************************************
* $Workfile: HttpUtils.java $
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* $Header: /3 开发工作目录/IM1.2/classes/com/neusoft/im2/util/HttpUtils.java 2 03-03-21 9:15 姬海涵 $
*****************************************************************************
*/
package cn.com.syntc.common.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.activation.FileTypeMap;
import javax.servlet.http.HttpServletResponse;
/**
* @version $Revision: 2 $
* @author <a href="mailto:jihh@neusoft.com">姬海涵</a>
*/
public class HttpUtils
extends javax.servlet.http.HttpUtils
{
//~ Instance/static variables ..................................................................
private static String charset = System.getProperty("file.encoding", "GBK");
private static SimpleDateFormat cookieDate;
private static SimpleDateFormat httpDateFormat;
//~ Initializers ...............................................................................
static
{
httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
cookieDate = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss zz", Locale.US);
cookieDate.setTimeZone(TimeZone.getTimeZone("GMT"));
}
//~ Methods ....................................................................................
/**
* 这个方法设置HTTP有效期头信息。符合HTTP 1.0 和 HTTP 1.1 标准。
*
* @param resp RunData
* @param expiry 过期秒数,<code>0</code> 表示立即过期(i.e. 不缓存)
*/
public static void setCacheHeaders(HttpServletResponse resp, int expiry)
{
if (expiry == 0)
{
String date = formatHttpDate(new Date());
resp.setHeader("Date", date);
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
resp.setHeader("Expires", date);
// code from ca portal
// resp.setDateHeader("Expires", 0);
// resp.setHeader("Cache", "F");
// resp.setHeader("Expires", "0");
}
else
{
Date expiryDate = new Date(System.currentTimeMillis() + expiry);
resp.setHeader("Expires", formatHttpDate(expiryDate));
}
}
/**
* 检查指定的文件名是否为系统内有效的文件名。
*
* <P>
* 有效的规则是 <code>filename[.ext]</code>,其中文件名和后缀由[a-z, A-Z, 0-9, -, _]组成。扩展名可有可无。
* </p>
*
* @param name 要检查的文件名
* @return 满足规则返回TRUE,否则返回FALSE。
*/
public static boolean isValidName(String name)
{
boolean result = false;
try
{
result = Pattern.matches("^[\\w-]+(\\.[\\w-]*)?$", name);
}
catch (PatternSyntaxException pse) {}
return result;
}
/**
* 依据RFC1123格式化日期样式。
*
* @param date 日期对象
* @return String
*/
public static String formatHttpDate(Date date)
{
synchronized (httpDateFormat)
{
return httpDateFormat.format(date);
}
}
/**
* 将一个指定的文件以HTTP协议送到客户端
*
* @param file 指定的文件
* @param response HttpServletResponse对象
* @throws IOException <!-- 请在这里添加异常描述 -->
*/
public static void sendfile(File file, HttpServletResponse response)
throws IOException
{
if (!file.exists())
{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String filename = file.getName();
String contentType = FileTypeMap.getDefaultFileTypeMap()
.getContentType(filename);
try
{
filename = new String(filename.getBytes(charset), "ISO-8859-1");
}
catch (UnsupportedEncodingException e) {}
response.setContentType(contentType + "; name=" + filename);
response.setHeader("Content-Disposition", "inline; filename=" + filename);
InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();
int i;
while ((i = is.read()) != -1)
{
os.write(i);
}
is.close();
os.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -