📄 uploadbean.java
字号:
package upload;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/*******************************************************************
* Title: 文件上传类
* Description: 既能对文件进行上传,又能取得输入框的值,最多可同时上传255个文件
* 刘哲理
*********************************************************************/
public class UploadBean extends HttpServlet {
private String[] sourceFile = new String[255]; //源文件名
private String[] suffix = new String[255]; //每个文件的后缀名
private String canSuffix = ".gif.jpg.jpeg.png"; //可上传的文件后缀名
private String objectPath = "c:/"; //目标文件目录
private String[] objectFileName = new String[255]; //目标文件名
private ServletInputStream sis = null; //输入流
private String[] description = new String[255]; //描述状态
private String[] fileSize = new String[255]; //每个文件的大小
private String[] fileId = new String[255]; //每个文件的在数据库传入后建立的id数值
private long size = 100 * 1024; //限制大小
private int count = 0; //已传输文件数目
private byte[] b = new byte[4096]; //字节流存放数组
private boolean successful = true;
private Hashtable fields = new Hashtable(); //存储控件的值
private Hashtable filefields = new Hashtable(); //存储文件控件的值
private Hashtable fileothername = new Hashtable(); //存储文件控件更名后的值
public UploadBean() {
}
//设置可以上传文件的后缀名
public void setcanSuffix(String canSuffix) {
this.canSuffix = canSuffix;
}
//设置文件保存路径
public void setObjectPath(String objectPath) {
this.objectPath = objectPath;
}
//设置最大的文件上传大小
public void setSize(long maxSize) {
this.size = maxSize;
}
//根据包含路径的文件名字,正确解析出文件的文件名
public String getFilename(String fName) {
//传来的文件包含路径,格式为 E:\xxx\xxx.xxx
System.out.println("传入文件名:"+fName);
int k=0;
k = fName.lastIndexOf("\\");
String toreturn=fName.substring(k + 1);
System.out.println("传出文件名:"+toreturn);
return toreturn;
}
//取得表单元素值
public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) fields.get(fieldName);
}
//取得表单元素值
public String getFileFieldValue(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) filefields.get(fieldName);
}
//取得表单元素值
public String getFileotherName(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) fileothername.get(fieldName);
}
//取得上传文件数
public int getCount() {
return count;
}
//取得目标路径
public String getObjectPath() {
return objectPath;
}
//取得源文件名
public String[] getSourceFile() {
return sourceFile;
}
//取得目标文件名
public String[] getObjectFileName() {
return objectFileName;
}
//取得上传状态描述
public String[] getDescription() {
return description;
}
//取得文件大小列表
public String[] getfileSize() {
return fileSize;
}
//取得文件在数据库中对应的id
public String[] getfileId() {
return fileId;
}
//取得文件的后缀名字列表
public String[] getsuffix() {
return suffix;
}
//文件上传处理程序,上传到硬盘文件,需要进行该方法的调用
//该方法的功能是解析http报头,到文件正文内容前
public void setSourceFile(HttpServletRequest request) throws IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1)
{
s = new String(b, 0, a);
//如果读取的字符串包含filename,则开始进行文件传输
if ( (k = s.indexOf("filename=\"")) != -1)
{
//这里还需要把提交数据的控件名获得
//Content-Disposition: form-data; name="userfile1"; filename="E:\s"
int b= s.indexOf("name=\"");
int c=s.indexOf("\";");
String fieldName = s.substring(b+6, c);
System.out.println("fieldName:"+fieldName);
String fieldValue;
// 取得文件数据
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
//将文件名字赋值
fieldValue=getFilename(s);
System.out.println("filedvalue:"+s);
filefields.put(fieldName, fieldValue);
//获取文件名,现在得到的是包含本机路径的文件名,调用getFilename()方法
sourceFile[count] = getFilename(s);
k = s.lastIndexOf(".");
suffix[count] = s.substring(k + 1);
if (canTransfer(count)) {
//进行文件传输,调用transferFile()方法
transferFile(fieldName,count);
}
++count;
} else if ( (k = s.indexOf("name=\"")) != -1) {
// 普通表单输入元素,获取输入元素名字
String fieldName = s.substring(k+6, s.length()-3);
sis.readLine(b, 0, b.length);
StringBuffer fieldValue = new StringBuffer(b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
} else {
fieldValue.append(s);
}
}
fields.put(fieldName, fieldValue.toString());
}
if (!successful)
break;
}
}
//判断上传文件的类型
private boolean canTransfer(int i) {
suffix[i] = suffix[i].toLowerCase();
//判断后缀名是否在满足的范围内
if (sourceFile[i].equals("") || (!(canSuffix.indexOf("."+suffix[i])>=0))) {
description[i] = "错误:文件后缀名错误";
return false;
}
else {
return true;
}
}
//上传文件转换
private void transferFile(String fieldName,int i) {
String x = Long.toString(new java.util.Date().getTime());
try {
objectFileName[i] = x + "." + suffix[i];
//保存文件的另存的名字
String othername=x + "." + suffix[i];
fileothername.put(fieldName,othername);
System.out.println("文件另存的名字为:"+othername);
FileOutputStream out = new FileOutputStream(objectPath + objectFileName[i]);
int a = 0;
int k = 0;
long hastransfered = 0; //标示已经传输的字节数
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("Content-Type:")) != -1) {
break;
}
}
sis.readLine(b, 0, b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
}
out.write(b, 0, a);
hastransfered += a;
if (hastransfered >= size) {
description[i] = "错误:文件 " + sourceFile[i] +" 太大,无法传输。";
successful = false;
break;
}
}
if (successful) {
description[i] = "正确 " + sourceFile[i] +" 被正确传输.";
fileSize[i]=String.valueOf(hastransfered);
}
out.close();
if (!successful) {
sis.close();
File tmp = new File(objectPath + objectFileName[count]);
tmp.delete();
}
}
catch (IOException ioe) {
description[i] = ioe.toString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -