package com.vogoal.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
/*
* vogoalAPI 1.0
* Auther SinNeR@blueidea.com
* by vogoal.com
* mail: vogoals@hotmail.com
*/
/**
* JSP上传文件类
*
* @author SinNeR
* @version 1.0
*/
public class JspFileUpload {
/** request对象 */
private HttpServletRequest request = null;
/** 上传文件的路径 */
private String uploadPath = null;
/** 每次读取得字节的大小 */
private static int BUFSIZE = 1024 * 8;
/** 存储参数的Hashtable */
private Hashtable paramHt = new Hasptable();
/** 存储上传的文件的文件名的ArrayList */
private ArrayList updFileArr = new ArrayList();
/**
* 设定request对象。
*
* @param request
* HttpServletRequest request对象
*/
public void setRequest(HttpServletRequest request) {
this.request = request;
}
/**
* 设定文件上传路径。
*
* @param path
* 用户指定的文件的上传路径。
*/
public void setUploadPath(String path) {
this.uploadPath = path;
}
/**
* 文件上传处理主程序。�������B
*
* @return int 操作结果 0 文件操作成功;1 request对象不存在。 2 没有设定文件保存路径或者文件保存路径不正确;3
* 没有设定正确的enctype;4 文件操作异常。
*/
public int process() {
int status = 0;
// 文件上传前,对request对象,上传路径以及enctype进行check。
status = preCheck();
// 出错的时候返回错误代码。
if (status != 0)
return status;
try {
// ��参数或者文件名�u��
String name = null;
// 参数的value
String value = null;
// 读取的流是否为文件的标志位
boolean fileFlag = false;
// 要存储的文件。
File tmpFile = null;
// 上传的文件的名字
String fName = null;
FileOutputStream baos = null;
BufferedOutputStream bos = null;
// ��存储参数的Hashtable
paramHt = new Hashtable();
updFileArr = new ArrayList();
int rtnPos = 0;
byte[] buffs = new byte[BUFSIZE * 8];
// �取得ContentType
String contentType = request.getContentType();
int index = contentType.indexOf("boundary=");
String boundary = "--" + contentType.substring(index + 9);
String endBoundary = boundary + "--";
// �从request对象中取得流。
ServletInputStream sis = request.getInputStream();
// 读取1行
while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
String strBuff = new String(buffs, 0, rtnPos);
// 读取1行数据�n��
if (strBuff.startsWith(boundary)) {
if (name != null && name.trim().length() > 0) {
if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
updFileArr.add(fName);
} else {
Object obj = paramHt.get(name);
ArrayList al = new ArrayList();
if (obj != null) {
al = (ArrayList) obj;
}
al.add(value);
System.out.println(value);
paramHt.put(name, al);
}
}
name = new String();
value = new String();
fileFlag = false;
fName = new String();
rtnPos = sis.readLine(buffs, 0, buffs.length);
if (rtnPos != -1) {
strBuff = new String(buffs, 0, rtnPos);
if (strBuff.toLowerCase().startsWith(
"content-disposition: form-data; ")) {
int nIndex = strBuff.toLowerCase().indexOf(
"name=\"");
int nLastIndex = strBuff.toLowerCase().indexOf(
"\"", nIndex + 6);
name = strBuff.substring(nIndex + 6, nLastIndex);
}
int fIndex = strBuff.toLowerCase().indexOf(
"filename=\"");
if (fIndex != -1) {
fileFlag = true;
int fLastIndex = strBuff.toLowerCase().indexOf(
"\"", fIndex + 10);
fName = strBuff.substring(fIndex + 10, fLastIndex);
fName = getFileName(fName);
if (fName == null || fName.trim().length() == 0) {
fileFlag = false;
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
continue;
}else{
fName = getFileNameByTime(fName);
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
}
}
}
} else if (strBuff.startsWith(endBoundary)) {
if (name != null && name.trim().length() > 0) {
if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
updFileArr.add(fName);
} else {
Object obj = paramHt.get(name);
ArrayList al = new ArrayList();
if (obj != null) {
al = (ArrayList) obj;
}
al.add(value);
paramHt.put(name, al);
}
}
} else {
if (fileFlag) {
if (baos == null && bos == null) {
tmpFile = new File(uploadPath + fName);
baos = new FileOutputStream(tmpFile);
bos = new BufferedOutputStream(baos);
}
bos.write(buffs, 0, rtnPos);
baos.flush();
} else {
System.out.println("test :" + value + "--" + strBuff);
value = value + strBuff;
}
}
}
} catch (IOException e) {
status = 4;
}
return status;
}
private int preCheck() {
int errCode = 0;
if ( request == null )
return 1;
if ( uploadPath == null || uploadPath.trim().length() == 0 )
ret
没有合适的资源?快使用搜索试试~ 我知道了~
JSP在线订餐网(SQL2000)
共213个文件
jpg:85个
jsp:62个
gif:43个
5星 · 超过95%的资源 需积分: 10 13 下载量 58 浏览量
2009-06-29
17:31:39
上传
评论
收藏 2.96MB RAR 举报
温馨提示
JSP在线订餐网(SQL2000)我也是网上找的 大家看看吧
资源推荐
资源详情
资源评论
收起资源包目录
JSP在线订餐网(SQL2000) (213个子文件)
JspFileUpload.class 6KB
JspFileUpload.class 6KB
JspFileUpload.class 6KB
connDB.class 2KB
connDB.class 2KB
chStr.class 680B
chStr.class 680B
bookelement.class 351B
foodelement.class 285B
foodelement.class 285B
style.css 2KB
index_06.gif 103KB
cart.gif 18KB
index_09.gif 11KB
VBxxxtkfsljx.gif 9KB
VFP200l.gif 8KB
ASPxxxtkfsljx.gif 8KB
VBgcyyysj.gif 8KB
Delphigcyyysj.gif 7KB
JSPgcyyysj.gif 7KB
VCgcyyysj.gif 7KB
ASPgcyyysj.gif 7KB
ASPsjkkfsljx.gif 7KB
Delphisjkkfsljx.gif 6KB
index_10.gif 6KB
PBsjkkfsljx.gif 6KB
VFPsjkkfsljx.gif 6KB
VBsjkkfsljx.gif 6KB
checkout.GIF 4KB
ico_cart.gif 4KB
ico_order.gif 4KB
index_03.gif 3KB
index_14.gif 3KB
index_18.gif 3KB
index_16.gif 3KB
reg.gif 3KB
bg_booksort.gif 3KB
index_ht.gif 2KB
index_12.gif 2KB
ico01.gif 2KB
T_mrbook.gif 1KB
ico_booksort.gif 1KB
freeze.gif 982B
folder.gif 933B
bg_Navigate.GIF 600B
thaw.gif 567B
enforce.gif 567B
greendot.gif 367B
Nav_separate.gif 261B
boardlist.gif 164B
modify.gif 121B
del.gif 109B
Cen_separate.gif 79B
bg.gif 52B
msbase.jar 280KB
sqljdbc.jar 247KB
mssqlserver.jar 65KB
msutil.jar 58KB
JspFileUpload.java 10KB
JspFileUpload.java 7KB
connDB.java 4KB
chStr.java 788B
foodelement.java 364B
66.jpg 279KB
65.jpg 252KB
dt2.jpg 174KB
Visual FoxProglxxxtwzxmslpx.jpg 142KB
VBglxxxtwzxmslpx.jpg 139KB
VCglxxxtwzxmslpx.jpg 135KB
PBglxxxtwzxmslpx.jpg 135KB
dt1.jpg 132KB
index_06.jpg 73KB
du3.jpg 54KB
login_M.jpg 43KB
PB200l.jpg 37KB
Delphixxxtkfsljx.jpg 31KB
014.jpg 5KB
023.jpg 5KB
022.jpg 5KB
066.jpg 5KB
007.jpg 5KB
017.jpg 5KB
060.jpg 5KB
028.jpg 5KB
020.jpg 5KB
015.jpg 5KB
044.jpg 5KB
013.jpg 5KB
016.jpg 5KB
019.jpg 5KB
048.jpg 5KB
021.jpg 5KB
018.jpg 5KB
045.jpg 5KB
067.jpg 5KB
001.jpg 5KB
024.jpg 5KB
010.jpg 5KB
026.jpg 5KB
054.jpg 5KB
共 213 条
- 1
- 2
- 3
资源评论
- _shot2014-06-03在学校看见系统两个就恶心
yodlove
- 粉丝: 12
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功