📄 fileupload.java
字号:
package com.singnet.util;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileUpload {
private int fieldNumber = 20; //上传文件或者文本输入框的数目
private String[] sourceFileName = new String[fieldNumber]; //源文件名
private String[] sourceFileExt = new String[fieldNumber]; //文件后缀名
private String allow = ".gif.jpg.jpeg.zip.rar"; //可上传的文件后缀名
private String savePath = "."; //目标文件目录
private String[] objectFileName = new String[fieldNumber]; //目标文件名
private String[] fileField = new String[fieldNumber]; //保存文件表单里对应的文本框的name值
private ServletInputStream inStream = null; //输入流
private String[] fileMessage = new String[fieldNumber]; //文件上传错误消息
private long size = 100 * 1024; //限制文件大小
private int fileCount = 0; //已传输文件数目,包括不符合类型和大小的,但这类文件不会上传
private byte[] b = new byte[4096]; //字节流存放数组
private boolean ok = true; //文件上传状态
private String[] textField = new String[fieldNumber]; //文本字段
private String[] textFieldValue = new String[fieldNumber]; //文本字段值
private String[] textMessage = new String[fieldNumber]; //表单处理错误消息
private int textCount = 0; //处理的文本框数目
public String contextPath = ""; //当前context对应的绝对路径
//Constractor
public FileUpload() {
}
public void init(javax.servlet.jsp.PageContext pageContext) {
if (pageContext == null) {
return;
}
String path = pageContext.getServletContext().getRealPath("/");
path = path.replace('\\', '/');
int length = path.length();
if (path.charAt(length - 1) == '/') {
path = path.substring(0, length - 1);
}
this.contextPath = path;
}
//设置输入框的数目
public void setFieldNumber(int n) {
this.fieldNumber = n;
}
//设置允许上传的文件后辍
public void setAllow(String allow) {
this.allow = allow;
}
//设置上传的文件保存路径
public void setSavePath(String path) {
this.savePath = path;
}
//设置文件最大字节数
public void setSize(long s) {
this.size = s;
}
//获得允许上传的文件后辍
public String getAllow() {
return this.allow;
}
//获得上传的文件保存路径
public String getSavePath() {
return this.savePath;
}
//获得上传文件个数
public int getFileCount() {
return this.fileCount;
}
//获得File域数组
public String[] getFileField() {
return this.fileField;
}
//获得上传后文件名数组
public String[] getObjectFileName() {
return this.objectFileName;
}
//获得源文件名数组
public String[] getSourceFileName() {
return this.sourceFileName;
}
//获得上传文件信息
public String[] getFileMessage() {
return this.fileMessage;
}
//获得文本域个数
public int getTextCount() {
return this.textCount;
}
//获得文本域name数组
public String[] getTextField() {
return this.textField;
}
//获得文本域value数组
public String[] getTextFieldValue() {
return this.textFieldValue;
}
//获得文本域信息
public String[] getTextMessage() {
return this.textMessage;
}
//处理ServletInputStream
public void doStream(HttpServletRequest request) throws IOException {
this.inStream = request.getInputStream();
int readLength = 0;
int offset = 0;
String str = "";
int a = 0;
int b = 0;
String formName = "";
boolean fileExtOk = false;
String formTextName = "";
while ((readLength = this.inStream.readLine(this.b, 0, this.b.length)) !=
-1) { //readLength为实际读入b的字节数,-1表示到达流末端
str = new String(this.b, 0, readLength);
//文件
if ((offset = str.indexOf("filename=\"")) != -1) {
//获得上传文件Form中对应的输入框的变量名
a = str.indexOf("name=\"");
formName = str.substring(a + 6);
b = formName.indexOf("\"");
formName = formName.substring(0, b);
this.fileField[this.fileCount] = formName;
//获得得源文件名
str = str.substring(offset + 10);
b = str.indexOf("\"");
str = str.substring(0, b);
if (str.equals("") || str == "") {
this.fileField[this.fileCount] = "";
continue;
}
this.sourceFileName[this.fileCount] = str;
//获得文件扩展名
a = this.sourceFileName[this.fileCount].lastIndexOf(".");
this.sourceFileExt[this.fileCount] = this.sourceFileName[this.
fileCount].substring(a + 1);
//检查文件扩展名
fileExtOk = this.checkFileExt(this.fileCount);
if (fileExtOk) {
this.doUpload(this.fileCount);
this.fileCount++;
} else {
this.objectFileName[fileCount] = "none";
this.fileCount++;
continue;
}
}
//文本域
else if ((offset = str.indexOf("name=\"")) != -1) {
//获得表单中的name值
formTextName = str.substring(offset + 6);
a = formTextName.indexOf("\"");
formTextName = formTextName.substring(0, a);
this.textField[textCount] = formTextName;
this.getInputValue(textCount);
this.textCount++;
} else {
continue;
}
}
}
//检查文件扩展名是否符合要求
public boolean checkFileExt(int fileCount) {
if ((this.allow.indexOf(sourceFileExt[fileCount])) != -1) {
return true;
} else {
this.fileMessage[fileCount] = "上传文件错误:不允许上传该类型的文件!";
return false;
}
}
//上传文件
public boolean doUpload(int count) {
//获得当前时间,作为上传后的文件名
String objFileName = Long.toString(new java.util.Date().getTime()) +
String.valueOf(count);
try {
this.objectFileName[count] = objFileName + "_bak." +
this.sourceFileExt[count];
File objFile = new File(this.contextPath + "/" + this.savePath,
this.objectFileName[count]);
//如果文件已存在
if (objFile.exists()) {
//再次调用自己
//再次调用可发以重新生成文件名
this.doUpload(count);
} else {
objFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(objFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int readLength = 0;
int offset = 0;
String str = "";
long readSize = 0L; //已经读取的大小,用以判断文件大小是否超过规定长度
//将指针移至Content-Type:这一行
while ((readLength = this.inStream.readLine(this.b, 0,
this.b.length)) != -1) {
str = new String(this.b, 0, readLength);
if (str.indexOf("Content-Type:") != -1) {
break;
}
}
//读空白行
this.inStream.readLine(this.b, 0, this.b.length);
while ((readLength = this.inStream.readLine(this.b, 0, b.length)) !=
-1) {
//检查文件是否已经读取完成
str = new String(this.b, 0, readLength);
if (this.b[0] == 45 && this.b[1] == 45 && this.b[2] == 45 &&
this.b[3] == 45 && this.b[4] == 45) {
break;
}
//写入文件
bos.write(this.b, 0, readLength);
readSize += readLength;
if (readSize > this.size) {
this.fileMessage[count] = "上传文件错误:文件大小超过限制!";
this.ok = false;
break;
}
}
if (this.ok) { //上传成功
bos.flush();
bos.close();
//读入已上传文件中的内容,去掉未尾的"\n"
int fileLength = (int) (objFile.length());
byte[] bb = new byte[fileLength - 2];
FileInputStream fis = new FileInputStream(objFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(bb, 0, (fileLength - 2));
fis.close();
bis.close();
//输出去掉"\n"后的文件
this.objectFileName[count] = objFileName + "." +
this.sourceFileExt[count];
File ok_file = new File(this.contextPath + "/" + this.savePath,
this.objectFileName[count]);
ok_file.createNewFile();
BufferedOutputStream bos_ok = new BufferedOutputStream(
new FileOutputStream(ok_file));
bos_ok.write(bb);
bos_ok.close();
objFile.delete();
this.fileMessage[count] = "OK";
return true;
} else { //失败
bos.flush();
bos.close();
File delFile = new File(this.contextPath + "/" + this.savePath,
this.objectFileName[count]);
delFile.delete();
this.objectFileName[count] = "none";
return false;
}
} catch (Exception e) {
this.objectFileName[count] = "none";
this.fileMessage[count] = e.toString();
return false;
}
}
//获得Input内容
public boolean getInputValue(int count) {
String str = "";
int readLength = 0;
try {
this.inStream.readLine(this.b, 0, this.b.length);
StringBuffer formTextValue = new StringBuffer();
//获得值
while ((readLength = this.inStream.readLine(this.b, 0,
this.b.length)) != -1) {
str = new String(this.b, 0, readLength);
if (this.b[0] == 45 && this.b[1] == 45 && this.b[2] == 45 &&
this.b[3] == 45 && this.b[4] == 45) {
break;
}
formTextValue.append(str);
}
str = formTextValue.toString();
this.textFieldValue[count] = str.substring(0, str.length() - 2);
this.textMessage[count] = "OK";
return true;
} catch (Exception e) {
this.textFieldValue[count] = "td_none";
this.textMessage[count] = "获得文本字段错误" + e.toString();
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -