国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

UploadBean源代碼

2019-11-18 15:11:05
字體:
來源:轉載
供稿:網友

  import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class UploadBean {
ServletRequest request;
ServletInputStream input;
String objectDir="c:/upload/";
PRivate int m_currentIndex;
private int MAX_FILE_SIZE=50*1024*1024;
private byte[] m_binaries;
private String m_boundary;
private int contentLength;
private String fileName="";

public UploadBean(){
super();
m_currentIndex=0;
}
public UploadBean(ServletRequest request){
this();
this.setRequest(request);
}
public void setRequest(ServletRequest request){
if (request!=null){
this.request=request;
try{
this.setInputStream(request.getInputStream());
}catch(IOException ioe){
System.out.println("IOException occurred in com.javacat.jsp.beans.upload.UploadBean.setRequest:"+ioe.getMessage());
}
}
}

public ServletRequest getRequest(){
return this.request;
}

public void setInputStream(ServletInputStream inputStream){
this.input=inputStream;
}

public ServletInputStream getInputStream(){
return this.input;
}

public OutputStream getOutputStream(String filename) throws IOException{
int findex=filename.lastIndexOf("/");
File file=new File(getObjectDir(), filename.substring(findex+1));
this.fileName=filename.substring(findex+1);
return new FileOutputStream(file);
}

public void setObjectDir(String dir){
this.objectDir=dir;
}
public String getObjectDir(){
return this.objectDir;
}
public String getFileName(){
return this.fileName;
}
public int upload() throws IOException,SecurityException{
if(request==null)
return -2;
boolean isFile;
String dataHeader;
String fileName="";
byte[] theBytes;
int countFile=0;
OutputStream output;
contentLength =request.getContentLength();

m_binaries=new byte[contentLength];
int haveRead=0;
while (haveRead<contentLength) {
haveRead+=getInputStream().read(m_binaries, haveRead, contentLength-haveRead);
}
//System.out.println("content= "+new String(m_binaries));
boolean match=false;
m_boundary=new String();
for (; !match && m_currentIndex<contentLength; m_currentIndex++ ){
if(m_binaries[m_currentIndex]==´´)
match=true;
else
m_boundary=m_boundary+(char)m_binaries[m_currentIndex];
}
if (m_currentIndex==1)
return -1;
m_currentIndex++;
do{
if(m_currentIndex>=contentLength)
break;
dataHeader=getDataHeader();
m_currentIndex=m_currentIndex+2;
isFile=dataHeader.indexOf("filename")>0;
if (isFile) {
fileName="";
if(!getFilePath(dataHeader).equals(""))
fileName=getFileName(getFilePath(dataHeader));
if((fileName==null)(fileName.equals("")))
isFile=false;
}
theBytes=getDataSection();
if(isFile){
if(theBytes.length>this.MAX_FILE_SIZE)
throw new SecurityException("File Size is too large.10M bytes is a limited");
else{
output=getOutputStream(fileName);
output.write(theBytes);
countFile++;
output.close();
}
}
if ((char)m_binaries[m_currentIndex+1]==´-´)
break;
m_currentIndex=m_currentIndex+2;
}while(true);
return countFile;
}

private byte[] getDataSection(){
int searchPosition=m_currentIndex;
int keyPosition=0;
int boundaryLen=m_boundary.length();
int start=m_currentIndex;
int end=m_currentIndex;
do{
if(searchPosition>=contentLength )
break;
if(m_binaries[searchPosition]==(byte)m_boundary.charAt(keyPosition)){
if(keyPosition==boundaryLen-1){
end=searchPosition - boundaryLen - 2;
break;
}
searchPosition++;
keyPosition++;
}
else{
searchPosition++;
keyPosition=0;
}
}while(true);
m_currentIndex=end+boundaryLen+3;
byte[] data=new byte[end-start+1];
for(int i=0; i<data.length; i++)
data[i]=m_binaries[start+i];
return data;
}

private String getDataHeader(){
int start =m_currentIndex;
int end=0;
int len=0;
boolean match=false;
while(!match){
if(m_binaries[m_currentIndex]==´

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁陕县| 池州市| 水城县| 五常市| 汤阴县| 平果县| 车致| 荆州市| 平乐县| 桐城市| 秦皇岛市| 大田县| 北安市| 蒲江县| 通河县| 鞍山市| 泸溪县| 益阳市| 乃东县| 墨江| 门头沟区| 时尚| 西城区| 水富县| 邯郸县| 赞皇县| 张北县| 南安市| 绿春县| 贡山| 陆河县| 潞西市| 金昌市| 定州市| 蒙城县| 平定县| 盐源县| 赤峰市| 麻阳| 普格县| 神池县|