jsp文件上传的一个类

最进上网找了个jsp文件上传的类。拿来大家一同讨论下。
import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletInputStream;

public class UpLoadBean {
private String SavePath = null;
private String FileName = null;
private String Sperator = null;
private String ErrMsg = "正常返回";

private byte[] BT = new byte[4096];
private byte[] preBT = new byte[4096];
private int t = -1;
private long FileSize = -1;
private boolean UserDefineFileName = false;

/
public UpLoadBean () {
FileName = bringFileName ();
}
public UpLoadBean (String savepath) {
SavePath = savepath;
FileName = bringFileName ();
}

/
// 文件名信息
private String bringFileName () {
Calendar dt = Calendar.getInstance ();
String str = "" + dt.get (Calendar.YEAR) + dt.get (Calendar.MONTH) + dt.get (Calendar.DAY_OF_MONTH);
str = str + dt.get (Calendar.HOUR) + dt.get (Calendar.MINUTE) + dt.get (Calendar.SECOND);
return str;
}

public void setFileName (String filename) {
FileName = filename;
UserDefineFileName = true;
}
public String getFileName () {
return FileName;
}
/
// 上传文件主程序
public boolean UpLoad (HttpServletRequest request,String url) throws Exception {
if (SavePath == null) {
setErrorMessage (url);//"未设置保存上传文件的路径"
return false;
}
request.setCharacterEncoding("utf-8");
ServletInputStream in = request.getInputStream ();

// 获取得一行的分隔标志
t = in.readLine (BT, 0, BT.length);
if (t != -1) {
Sperator = new String (BT, 0, t);
Sperator = Sperator.substring (0, 28);
t = -1;
}


// 取扩展名并设定文件全名
do {
t = in.readLine (BT, 0, BT.length);
String s = new String (BT, 0, t);
int index = s.indexOf ("filename=\"");
if (index != -1) {
s = s.substring (index + 10);
index = s.indexOf ("\"");
s = s.substring (0, index);
index = s.lastIndexOf (".");
if (!UserDefineFileName) {

FileName = FileName + s.substring (index); // 设定文件全名
}
t = -1;
}
} while (t != -1);


// 取文件内容
FileOutputStream out = new FileOutputStream (SavePath + "\\" + FileName);

t = in.readLine (BT, 0, BT.length);
String s = new String (BT, 0, t);
int i = s.indexOf ("Content-Type:");
if (i == -1) {
setErrorMessage ("上传的不是文件");
return false;
} else {
in.readLine (BT, 0, BT.length); // 去掉一个空行
t = -1;
}

boolean state = true;
long trancsize = 0;
try {
t = in.readLine (BT, 0, BT.length);

while (t != -1) {
s = new String (BT, 0, t);
if (s.length () > 28) {
s = s.substring (0, 28);
if (s.equals (Sperator)) {
break;
}
}

/*
if (BT[0] == 45 && BT[1] == 45 && BT[2] == 45 && BT[3] == 45 && BT[4] == 45) {
break;
}
*/
if (FileSize != -1) {
if (trancsize >= FileSize) {
throw new Exception ("上传文件太大");
}
}
out.write (BT, 0, t);
trancsize += t;
t = in.readLine (BT, 0, BT.length);
}
} catch (Exception e) {
state = false;
out.close ();
setErrorMessage (e.getMessage ());
File tmp = new File (SavePath, FileName);
if (!tmp.delete()) {
setErrorMessage (e.getMessage () + ",清除未完成");
}
} finally {
if (state) {
out.close ();
}
return state;
}
}
/
// 上传文件错误信息
public void setErrorMessage (String errormessage) {
ErrMsg = errormessage;
}
public String getErrorMessage () {
return ErrMsg;
}
/

// 文件大小信息
public void setFileSize (long size) {
FileSize = size;
}

public long getFileSize () {
return FileSize;
}
}// end class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值