package edu.cqu.common;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
public class MultipartRequest {
private static Logger log = Logger.getLogger(MultipartRequest.class);
private String charset = "UTF-8";
private Hashtable map = new Hashtable();
private HttpServletRequest request;
private List fileItems = new ArrayList();
private boolean isMultipart;
public MultipartRequest(HttpServletRequest request) {
this(request, -1, -1, null);
}
// 通过构造函数限定附件使用的内存大小,最大的附件大小和临时目录
public MultipartRequest(HttpServletRequest request, int maxMemorySize,
int maxFileSize, String tempDirectory) {
this.charset = request.getCharacterEncoding();
log.info("charset:" + this.charset);
this.request = request;
// 判断该request对象是否包含多个部分,是否可以包含附件
isMultipart = ServletFileUpload.isMultipartContent(request);
ServletFileUpload upload;
DiskFileItemFactory factory;
if (isMultipart) {
// 使用FileUpload组件中的类
factory = new DiskFileItemFactory();
if (maxMemorySize > 10 * 1024)
factory.setSizeThreshold(maxMemorySize);
if (tempDirectory != null)
factory.setRepository(new File(tempDirectory));
upload = new ServletFileUpload(factory);
if (maxFileSize > 0)
upload.setSizeMax(maxFileSize);
try {
fileItems = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
for (int i = 0; i < fileItems.size(); i++) {
FileItem item = (FileItem) fileItems.get(i);
map.put(item.getFieldName(), item);
}
} else {
Enumeration enu = request.getParameterNames();
while (enu.hasMoreElements()) {
Object tmp = enu.nextElement();
if (tmp != null)
map.put(tmp.toString(), request
.getParameter(tmp.toString()));
}
}
}
public MultipartRequest(HttpServletRequest request, int maxMemorySize,
int maxFileSize) {
this(request, maxMemorySize, maxFileSize, null);
}
// 判断Request中是否包含name域
public boolean contains(String name) {
String value = getString(name);
return value != null;
}
public boolean equals(String name, String compareValue) {
String value = getString(name);
return (value != null) && value.equals(compareValue);
}
public boolean equalsIgnoreCase(String name, String compareValue) {
String value = getString(name);
return (value != null) && value.equalsIgnoreCase(compareValue);
}
// 从存储日期的域中,获得一个日期对象
public Date getDate(String name, String dateFormat) {
String value = null;
if (!isMultipart()) {
value = request.getParameter(name);
} else {
value = getFieldValueFromMultiypartRequest(name);
}
Date result = DateUtil.parse(dateFormat, value);
return result;
}
public double getDouble(String name) {
return getDouble(name, -1);
}
public double getDouble(String name, double defaultValue) {
String value = null;
if (!isMultipart()) {
value = request.getParameter(name);
} else {
value = getFieldValueFromMultiypartRequest(name);
}
double result = parseDouble(value);
result = (result == -1) ? defaultValue : result;
return result;
}
public double[] getDoubleArray(String name) {
String[] value = getStringArray(name);
double[] result = new double[value.length];
for (int i = 0; i < result.length; i++) {
result[i] = parseDouble(value[i]);
}
return result;
}
// 从request对象的文件域获得文件流
public InputStream getFielInputStream(String fieldName) throws Exception {
FileItem item = getFile(fieldName);
return ((item != null) & !item.isFormField()) ? item.getInputStream()
: null;
}
// 从request对象中,获得一个表示附件的FileItem对象
public FileItem getFile(String fieldName) {
return (FileItem) map.get(fieldName);
}
// 返回文件域中存储的文件名
public String getFileName(String fieldName) {
FileItem item = getFile(fieldName);
String fname = (item != null) ? item.getName() : null;
if ((fname != null) && (fname.length() > 0)) {
fname = fname.substring(fname.lastIndexOf(File.separatorChar) + 1);
}
return fname;
}
public String[] getFileNames() {
FileItem[] items = getFiles();
String[] result = new String[items.length];
for (int i = 0; i < items.length; i++) {
result[i] = items[i].getName();
if ((result[i] != null) && (result[i].length() > 0)) {
result[i] = result[i].substring(result[i]
.lastIndexOf(File.separatorChar) + 1);
}
}
return result;
}
public Set getParameterNamesSet() {
return map.keySet();
}
public Enumeration getParameterNames() {
return map.keys();
}
public long getFileSize(String fieldName) {
FileItem item = getFile(fieldName);
return (item != null) ? item.getSize() : 0;
}
// 返回文件域的数组
public FileItem[] getFiles() {
ArrayList arr = new ArrayList();
for (int i = 0; i < fileItems.size(); i++) {
FileItem item = (FileItem) fileItems.get(i);
if (!item.isFormField()) {
arr.add(item);
}
}
FileItem[] result = new FileItem[arr.size()];
return (FileItem[]) arr.toArray(result);
}
public float getFloat(String name) {
return getFloat(name, -1);
}
public float getFloat(String name, float defaultValue) {
String value = null;
if (!isMultipart()) {
value = request.getParameter(name);
} else {
value = getFieldValueFromMultiypartRequest(name);
}
float result = parseFloat(value);
result = (result == -1) ? defaultValue : result;
return result;
}
public float[] getFloatArray(String name) {
String[] value = getStringArray(name);
float[] result = new float[value.length];
for (int i = 0; i < result.length; i++) {
result[i] = parseFloat(value[i]);
}
return result;
}
public int getInt(String name) {
return getInt(name, -1);
}
public int getInt(String name, int defaultValue) {
String value = null;
if (!isMultipart()) {
value = request.getParameter(name);
} else {
value = getFieldValueFromMultiypartRequest(name);
}
int result = parseInt(value);
result = result == -1 ? defaultValue : result;
return result;
}
public int[] getIntArray(String name) {
String[] value = getStringArray(name);
int[] result = new int[value.length];
for (int i = 0; i < result.length; i++) {
result[i] = parseInt(value[i]);
}
return result;
}
public long getLong(String name) {
return getLong(name, -1);
}
public long getLong(String name, long defaultValue) {
String value = null;
if (!isMultipart()) {
value = request.getParameter(name);
} else {
value = getFieldValueFromMultiypartRequest(name);
}
long result = parseLong(value);
result = (result == -1) ? defaultValue : result;
return result;
}
public long[] getLongArray(String name) {
String[] value = getStringArray(name);
long[] result = new long[value.length];
for (int i = 0; i < result.length; i++) {
result[i] = parseLong(value[i]);
}
return result;
}
public boolean isMultipart() {
return isMultipart;
}
public String getString(String name) {
return getString(name, null);
}
public String getParameter(String name) {
return getString(name, null);
}
public String getString(String name, String defaultValue) {
String value = null;
if (!isMultipart()) {
value = request.getParameter(name);
} else {
value = getFieldValueFromMultiypartRequest(name);
}
value = (value == null) ? defaultValue : value;
return value;
}
public String[] getStringArra
没有合适的资源?快使用搜索试试~ 我知道了~
web文件管理系统
共188个文件
class:63个
java:61个
jsp:18个
5星 · 超过95%的资源 需积分: 50 127 下载量 111 浏览量
2019-02-23
10:07:44
上传
评论 8
收藏 1.12MB ZIP 举报
温馨提示
这是web开发的文件管理系统,是以Java语言开发的,欢迎各位需要的亲下载
资源推荐
资源详情
资源评论
收起资源包目录
web文件管理系统 (188个子文件)
.amateras 216B
MultipartRequest.class 11KB
MultipartRequest.class 11KB
FileManagerService.class 8KB
FileManagerService.class 7KB
FileDAOImpl.class 5KB
FileDAOImpl.class 5KB
UserDAOImpl.class 5KB
UserDAOImpl.class 5KB
FileDAOImpl.class 4KB
UserDAOImpl.class 4KB
DownloadFile.class 4KB
DownloadFile.class 4KB
File.class 3KB
File.class 3KB
User.class 3KB
User.class 3KB
RegUser.class 3KB
RegUser.class 3KB
DBPool.class 3KB
DBPool.class 3KB
ShowFileListOfUser.class 2KB
ShowFileListOfUser.class 2KB
UserLogin.class 2KB
UserLogin.class 2KB
UserDownloadFile.class 2KB
UserDownloadFile.class 2KB
ShowAllFileList.class 2KB
ShowAllFileList.class 2KB
DAOImpl.class 2KB
DAOImpl.class 2KB
DAOImpl.class 2KB
EncodingFilter.class 2KB
EncodingFilter.class 2KB
UserDeleteFile.class 2KB
UserDeleteFile.class 2KB
AdminDeleteUser.class 2KB
AdminDeleteUser.class 2KB
UploadFile.class 2KB
UploadFile.class 2KB
ShowAllUserList.class 2KB
ShowAllUserList.class 2KB
DateUtil.class 2KB
DateUtil.class 2KB
EntitySupport.class 1KB
EntitySupport.class 1KB
UserLogout.class 1KB
UserLogout.class 1KB
TestLog4j.class 1013B
FileManagerException.class 898B
FileManagerException.class 898B
Car.class 596B
Vehicle.class 595B
Test.class 512B
Constants.class 503B
Constants.class 503B
FileDAO.class 443B
UserDAO.class 366B
DAO.class 354B
Entity.class 354B
DAO.class 354B
Entity.class 354B
UserDAO.class 154B
FileDAO.class 154B
.classpath 706B
.classpath 526B
org.eclipse.wst.common.component 442B
org.eclipse.wst.common.component 420B
log4j-1.2.11.jar 342KB
log4j-1.2.11.jar 342KB
commons-io-1.1.jar 60KB
commons-io-1.1.jar 60KB
commons-logging-1.0.4.jar 37KB
commons-logging-1.0.4.jar 37KB
commons-fileupload-1.1.jar 31KB
commons-fileupload-1.1.jar 31KB
MultipartRequest.java 10KB
MultipartRequest.java 10KB
FileManagerService.java 6KB
FileManagerService.java 6KB
FileDAOImpl.java 5KB
FileDAOImpl.java 4KB
FileDAOImpl.java 4KB
UserDAOImpl.java 4KB
UserDAOImpl.java 4KB
UserDAOImpl.java 3KB
DownloadFile.java 3KB
DownloadFile.java 3KB
ShowAllFileList.java 2KB
ShowAllFileList.java 2KB
RegUser.java 2KB
RegUser.java 2KB
DBPool.java 2KB
DBPool.java 2KB
File.java 2KB
File.java 2KB
User.java 2KB
User.java 2KB
UserDeleteFile.java 2KB
UserDeleteFile.java 2KB
共 188 条
- 1
- 2
资源评论
- userNameyzj2019-05-15参考可以用!
huang18720963745
- 粉丝: 3
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功