package org.whvcse.upload;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import org.whvcse.upload.extend.Toolkit;
import org.whvcse.upload.impl.Upload;
/**
* 文 件 名 : jsp文件上传下载<br>
*
* @author 孟德军 CopyRright (c) 2009: <a href='mak0000@msn.com'>mak0000</a> <br>
* 文件编号: 20090526<br>
* @version 1.0 <br>
* 日 期: 2009.05.26 <br>
*
*/
public class JspUpload implements Upload {
private static final long serialVersionUID = 1L;
private HttpServletRequest request = null;
private BufferedWriter writer = null;
/**
* @see #fileSize 允许上传的文件大小
*/
private long fileSize = 1024;
/**
* @see #fileName 文件名字
*/
private String fileName = null;
/**
* @see #bufferSize 设置缓冲区的大小
*/
private int bufferSize = 128;
/**
* @see #savePath 文件保存的路径
*/
private String savePath = null;
private int t = -1;
/**
* @see #check 文件约束
*/
private String[] check = null;
/**
* @see #encoding 文件编码
*/
private String encoding = "utf-8";
/**
* @see #showErrorMessage 是否显示错误信息
*/
private boolean showErrorMessage = true;
private byte[] buffer = null;
private String seperator = null;
private boolean userDefineName = false;
private String extendname;
private Vector ErrorMessage = null;
private long length = 0;
private boolean isShowLog = false;
public JspUpload() {
}
public JspUpload(String savepath) {
this.savePath = savepath;
}
public JspUpload(HttpServletRequest request) {
this.request = request;
}
public JspUpload(HttpServletRequest request, String path) {
this.request = request;
this.savePath = path;
}
public boolean isShowLog() {
return isShowLog;
}
public void setShowLog(boolean isShowLog) {
this.isShowLog = isShowLog;
try {
if (this.isShowLog)
{
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(".//upload.log", true), System
.getProperty("file.encoding")));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* @deprecated
*/
public boolean destory() throws Exception {
if (writer != null)
{
writer.close();
}
return true;
}
public boolean upLoad(HttpServletRequest request) throws Exception {
boolean status = true;
if (savePath == null) {
errorMessage("未设置保存上传文件的路径!");
}
/*
* if (this.fileName == null) { this.fileName = setFileName(); }
*/
setRequest(request);
status = execute();
return status;
}
private void errorMessage(String message) throws Exception {
if (isShowLog) {
server("Exception" + message);
}
if (showErrorMessage) {
ErrorMessage = new Vector();
ErrorMessage.add(message);
throw new Exception(message);
}
}
public boolean upload() throws Exception {
boolean status = true;
/*
* if (this.fileName == null) { this.fileName = setFileName(); }
*/
if (this.request == null) {
errorMessage(this.request.toString() + "不能为空!");
return false;
} else {
status = execute();
}
return status;
}
public String getFileName() {
return fileName;
}
/**
* 设置上传的文件名,若不设置此项,程序将自动分析文件获得文件名,建议设置<br>
* eg: test.txt
*
* @param fileName
* 文件名
*/
public void setFileName(String fileName) {
this.fileName = fileName;
userDefineName = true;
}
public long getFileSize() {
return fileSize;
}
/**
* 设置文件允许上传文件大小,默认为1024
*
* @param fileSize
* 文件大小
*/
public void setFileSize(long fileSize) {
this.fileSize = fileSize * 1024;
}
public HttpServletRequest getRequest() {
return request;
}
/**
*
* @param request
* HttpServletRequest
*/
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public String getSavePath() {
return savePath;
}
/**
* 文件存储路径,必须设置此项.
*
* @param savePath
* 文件存储路径,必须设置此项.
*/
public void setSavePath(String savePath) {
this.savePath = savePath;
}
/**
*
* @return 返回约束数组.
*/
public String[] getCheck() {
return check;
}
/**
* 为上传文件添加约束,即不允许上传的文件类型<br>
* eg:exe bmp
*
* @param s
* 存放约束的字符串数组.若连续两次设置此项,将覆盖前一次设置.默认无.
* @return 已有的约束的数目.
*/
public int addCheck(String[] s) {
if (check == null) {
check = s;
}
return check.length;
}
private boolean execute() throws Exception {
FileOutputStream out = null;
boolean status = true;
try {
buffer = new byte[bufferSize];
request.setCharacterEncoding(getEncoding());
// 获取流对象.
ServletInputStream in = request.getInputStream();
// 获取一行标志.
t = in.readLine(buffer, 0, buffer.length);
if (t != -1) {
seperator = new String(buffer, 0, t);
seperator = seperator.substring(0, 28);
t = -1;
}
// 扩展名.filename="f:\exercise\c++.cpp"
do {
t = in.readLine(buffer, 0, buffer.length);
String s = new String(buffer, 0, t);
int index = s.indexOf("filename=\"");
if (index != -1) {
s = s.substring(index + 10);
// s=f:\exercise\c++.cpp"
index = s.indexOf("\"");
s = s.substring(0, index);
String temp = s;
if (!userDefineName) {
this.fileName = Toolkit.parse(temp, true);
// setFileName(Toolkit.parse(temp, false));
}
// s=f:\exercise\c++.cpp
index = s.lastIndexOf(".");
extendname = s.substring(index);
if (check != null) {
for (int i = 0; i < check.length; i++) {
if (extendname.equals("." + check[i])) {
errorMessage("上传的文件不符合规范,请重新上传!");
break;
}
}
}
/*
* if (!userDefineName) { this.fileName = this.fileName +
* s.substring(index); // s=.cpp }
*/
t = -1;
}
} while (t != -1);
int point = this.fileName.lastIndexOf(".");
if (point != -1) {
// 读取文件内容.
out = new FileOutputStream(this.savePath + "\\" + this.fileName);
} else {
out = new FileOutputStream(this.savePath + "\\" + this.fileName
+ extendname);
}
t = in.readLine(buffer, 0, buffer.length);
String s = new String(buffer, 0, t);
int i = s.indexOf("Content-Type:");
if (i == -1) {
errorMessage("上传的不是文件");
status = false;
return status;
} else {
// 去掉一个空行.
in.readLine(buffer, 0, buffer.length);
t = -1;
}
long trancsize = 0;
t = in.readLine(buffer, 0, buffer.length);
while (t != -1) {
s = new String(buffer, 0, t);
if (s.length() > 28) {
s = s.substring(0, 28);
if (s.equals(seperator)) {
break;
}
}
if (fileSize != -1) {
if (trancsize >= (getFileSize())) {
File f = new File(this.savePath + "\\" + this.fileName
+ extendname);
f.delete();
errorMessage("上传文件过大!");
}
}
out.write(buffer, 0, t);
trancsize += t;
t = in.readLine(buffer, 0, buffer.length);
}
length = trancsize;
} catch (Exception e) {
status = false;
File f = new File(this.savePath + "\\" + this.fileName + extendname);
f.delete();
没有合适的资源?快使用搜索试试~ 我知道了~
JspUpload JSP文件上传下载助手 完整版 Beta V1.0 附源码
共87个文件
html:69个
java:7个
jsp:3个
5星 · 超过95%的资源 需积分: 10 65 下载量 108 浏览量
2009-05-27
13:57:36
上传
评论
收藏 630KB RAR 举报
温馨提示
JspUpload JSP文件上传下载助手 正式版 修改了部分BUG 增加了配置文件web.xml 添加了156中常用的Mime类型.用户不需要配置Mime类型,增加配置文件即可.附源码!自动匹配Mime类型!
资源推荐
资源详情
资源评论
收起资源包目录
JspUnload.rar (87个子文件)
JspUnload
src
org
whvcse
upload
JspUpload.java 10KB
extend
Toolkit2.java 3KB
Toolkit.java 3KB
impl
Test.java 376B
Download.java 1KB
Upload.java 1KB
download
JspDownload.java 8KB
config
web.xml 19KB
JspUnload.jar 267KB
doc
resources
inherit.gif 57B
Thumbs.db 3KB
org
whvcse
upload
package-use.html 5KB
package-summary.html 6KB
class-use
JspUpload.html 6KB
old
package-use.html 5KB
package-summary.html 6KB
class-use
Upload.html 6KB
package-frame.html 918B
Upload.html 13KB
package-tree.html 6KB
package-frame.html 898B
extend
package-use.html 7KB
Toolkit2.html 13KB
package-summary.html 6KB
class-use
Toolkit2.html 6KB
Load.html 6KB
Upload.html 6KB
Toolkit.html 7KB
package-frame.html 1KB
Load.html 18KB
Upload.html 19KB
Toolkit.html 12KB
package-tree.html 6KB
impl
package-use.html 7KB
package-summary.html 7KB
Download.html 11KB
class-use
Download.html 7KB
Upload.html 7KB
Example.html 6KB
Test.html 6KB
package-frame.html 1KB
Upload.html 12KB
Example.html 10KB
Test.html 10KB
package-tree.html 6KB
JspUpload.html 29KB
package-tree.html 6KB
download
package-use.html 5KB
package-summary.html 6KB
class-use
JspDownload.html 6KB
package-frame.html 912B
JspDownload.html 30KB
package-tree.html 6KB
stylesheet.css 2KB
allclasses-noframe.html 1KB
overview-frame.html 1KB
allclasses-frame.html 1KB
overview-tree.html 7KB
serialized-form.html 11KB
constant-values.html 5KB
index-files
index-4.html 6KB
index-1.html 6KB
index-5.html 11KB
index-10.html 6KB
index-12.html 12KB
index-14.html 7KB
index-17.html 6KB
index-6.html 7KB
index-15.html 8KB
index-2.html 6KB
index-9.html 6KB
index-11.html 6KB
index-8.html 6KB
index-13.html 9KB
index-7.html 9KB
index-16.html 8KB
index-3.html 8KB
package-list 90B
help-doc.html 10KB
deprecated-list.html 7KB
index.html 1KB
overview-summary.html 6KB
eg
FileUpload
WEB-INF
lib
config
web.xml 19KB
JspUnload.jar 267KB
classes
index.jsp 1KB
loadFile.jsp 487B
show.jsp 1KB
共 87 条
- 1
资源评论
- lizs_bj2018-03-08怎么用呢?没有说明,还不知道 怎么使用?
小孟哥
- 粉丝: 56
- 资源: 52
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功