/**
* 上传文件. 此类型被实例化后,还必须调用此方法才能正确上传文件
*/
public void upload(){
SmartAccessFile raf = null;
// long time = System.currentTimeMillis();
try{
/* 创建临时文件 */
tempFile = java.io.File.createTempFile("httpUpload", ".tmp");
raf = new SmartAccessFile(tempFile, "rw");
/* 返回总字节数量 */
totalBytes = request.getServletRequest().getContentLength();
// logger.debug("流失时间: "+(System.currentTimeMillis()-time));
if (maxContentLength > (long)0 && totalBytes > maxContentLength){
request.getServletRequest().getInputStream().close();
throw new JThinkRuntimeException("Total request content size exceeded."); //请求的内容太大,不允许上传文件
}
/* 读入所有字节数据,并保存到临时文件 */
InputStream is = request.getServletRequest().getInputStream();
int readBytes = 1024*256;
if(totalBytes>1024*256 && totalBytes<=1024*512){
readBytes = 1024*512;
}else if(totalBytes>1024*512 && totalBytes<=1048576){
readBytes = 1048576;
}else if(totalBytes>1048576 && totalBytes<=1048576*5){
readBytes = 1048576*2;
}else if(totalBytes>=1048576*5){
readBytes = 1048576*5;
}
int tmpReadBytes = 0;
byte[] bs = new byte[readBytes];
for(int currBytes=0;currBytes<totalBytes;){
int count = is.read(bs, tmpReadBytes, readBytes-tmpReadBytes);
currBytes += count;
tmpReadBytes += count;
if(tmpReadBytes==readBytes || currBytes>=totalBytes){
raf.write(bs, 0, tmpReadBytes);
tmpReadBytes = 0;
}
}
/* 查找请求数据的开始索引位置和请求数据块间的分隔串 */
raf.seek(0);
int v = raf.read();
boundary = "";
while(v!=13 && raf.getFilePointer() < totalBytes){
boundary += (char)v;
v = raf.read();
}
/* 如果没有任何请求信息, 直接返回 */
if(raf.getFilePointer()==1 || raf.getFilePointer()>=raf.length()){
return;
}
while(true){
if (raf.getFilePointer() >= totalBytes){
break;
}
/* 返回字段头信息 */
String dataHeader = getDataHeader(raf);
boolean isFile = dataHeader.indexOf("filename") > 0;
String fieldName = getDataFieldValue(dataHeader, "name");
if (isFile) {
filePathName = getDataFieldValue(dataHeader, "filename");
fileName = preName + getFileName(filePathName);
fileExt = getFileExt(fileName);
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
/* 处理数据片段 */
getDataSection(raf);
/* 判断文件是否被允许上传 */
if (isFile && fileName.length() > 0) {
if (deniedFilesList!=null && deniedFilesList.contains(fileExt)){
throw new JThinkRuntimeException(
"The extension of the file is denied to be uploaded.");//不允许上传此种类型的文件
}
if (allowedFilesList!=null && !allowedFilesList.contains(fileExt)){
throw new JThinkRuntimeException(
"The extension of the file is not allowed to be uploaded.");//不允许上传此种类型的文件
}
if (maxFileSize > (long) 0 && (long) ((endData - startData) + 1) > maxFileSize){
throw new JThinkRuntimeException(String.valueOf(
(new StringBuffer("Size exceeded for this file : ")).append(fileName)));//文件太大,不允许上传
}
}
if (isFile) {
/* 生成被上传的单个临时文件 */
org.fto.jthink.j2ee.web.fileload.File newFile = new org.fto.jthink.j2ee.web.fileload.File();
newFile.setParent(this);
newFile.setFieldName(fieldName);
newFile.setFileName(fileName);
newFile.setFileExt(fileExt);
newFile.setFilePathName(filePathName);
newFile.setIsMissing(filePathName.length() == 0);
newFile.setContentType(contentType);
newFile.setContentDisp(contentDisp);
newFile.setTypeMIME(typeMIME);
newFile.setSubTypeMIME(subTypeMIME);
if (contentType.indexOf("application/x-macbinary") > 0){
startData = startData + 128;
}
newFile.setSize((endData - startData));
newFile.setStartData(startData);
newFile.setEndData(endData);
files.add(newFile);
} else {
/* 生成非文件的请求信息数据 */
long tmpPointer = raf.getFilePointer();
raf.seek(startData);
byte[] values = new byte[(endData - startData)];
raf.read(values);
String value;
if(encode==null){
value = new String(values);
}else{
value = new String(values, encode);
}
request.putParameter(fieldName, value);
raf.seek(tmpPointer);
}
raf.skipBytes(1);
char c = (char)raf.read();
/* 如果遇到'-'字符,跳出循环 */
if (c == '-'){
raf.skipBytes(-1);
break;
}
}