jsp技巧大全
[08-23 22:09:45] 来源:http://www.duoxue8.com JSP教程 阅读:128次//取哈希表中的数据
public String getFieldValue(String s) {
String temp="";
if(dic.containsKey(s)) //判断表中是否存在s键,不判断则返回nullpointerException
{
temp=(String)dic.get(s);
temp=temp.trim();
}else
temp="";
return temp;
}
////////////////////////////////////////////////
////用指定的编码方式生成字符串
public String newLine(byte oneLine[],int sp,int i,String charEncode)
throws java.io.UnsupportedEncodingException {
sp=0; // start position
String lineStr=null;
if (charEncode!=null) {
return lineStr=new String(oneLine,sp,i,charEncode); //用指定的编码方式生成字符串
}
else {
return lineStr=new String(oneLine,sp,i);
}
}
///////////////////////////////////////////////
//得到上传文件的大小
public int getTotalSize() {
return totalSize/1000;
}
///////////////////////////////////////
//删除指定路径的文件
public boolean delFiles(String fn) //fn为要删除的文件名,不包括路径
{
try
{
File file=new File(savePath+fn);
System.out.println(savePath+fn);
if(file.exists())
{
file.delete();
System.out.println(file.getPath()+"delete file successfully!");
return true;
}else
{
System.out.println("the file is not existed!");
return true;
}
}catch(Exception e)
{
System.out.println(e.toString());
return false;
}
}
////////////////////////////////////////////////
//文件列表
public String[] listFiles(String fp)
{
String[] lf=null;
try{
savePath=path+fp;
File file=new File(savePath);
lf=file.list(new DirFilter());
for(int i=0;i<lf.length;i++)
System.out.println(lf[i]);
}catch(Exception e){ e.printStackTrace();}
return lf;
}
/////////////////////////////////////////////////
//开始上传文件
public boolean doUpload(HttpServletRequest req)
throws java.io.IOException {
String fieldValue=""; //表单元素值
String fieldName=""; //表单元名称
int pos=-1; //临时变量,用于记录位置
int pos2=-1; //临时变量,用于记录位置
String lineStr=null; //用oneLine[]生成的每行字符串
byte oneLine[] =new byte[4096]; //用于每次读取的数据
FileOutputStream fos=null; //文件输出流
init(req); //初始化
ServletInputStream sis=req.getInputStream();
int i=sis.readLine(oneLine,0,oneLine.length); //返回实际读取的字符数,并把数据写到oneLine中
while (i!=-1) {
lineStr=newLine(oneLine,0,i,charEncode); //生成字符串
if (lineStr.indexOf(getBoundary()+"--")>=0)
break;
if (lineStr.startsWith("Content-Disposition: form-data; name="")) {
//分离数据,因为表单元素也一并上传,还有其它数据,对我们有用的只是
//文件的内容,表单元素及表单元素对应的值
if (lineStr.indexOf(""; filename="")>=0) { //是文件输入域
//设置文件名
setFileName(lineStr);
if (!fileName.equals("")) { //如果文件名为空则跳过
//提取表单元素名称及表单元素对应的值
pos=lineStr.indexOf("name="");
pos2=lineStr.indexOf(""; filename="");
//表单元素名字
fieldName=lineStr.substring(pos+6,pos2);
//表单元素值
fieldValue=lineStr.substring(pos2+13,lineStr.length()-3);
//加入哈希表中
dic.put(fieldName,fieldValue);
sis.readLine(oneLine,0,oneLine.length); //读取的数据类似"Content-Type: text/plain"
sis.readLine(oneLine,0,oneLine.length); //空行
//建立文件输出
fos=new FileOutputStream(new File(getSavePath(),getNewFileName()));
//开始读上传文件数据
i=sis.readLine(oneLine,0,oneLine.length);
while(i!=-1) {
totalSize=i+totalSize;
lineStr=newLine(oneLine,0,i,charEncode);
if (lineStr.indexOf(getBoundary())>=0)
break; //表明这个文件区的数据读取完毕
fos.write(oneLine,0,i);
i=sis.readLine(oneLine,0,oneLine.length);
}//end while
fos.close();
}//end if (!getFileName().equals(""))
}
else { //非文件输入域
pos=lineStr.indexOf("name="");
//表单元素名字
fieldName=lineStr.substring(pos+6,lineStr.length()-3);
//读空行
sis.readLine(oneLine,0,oneLine.length);
//这行含有元素值,如里元素值为空,则这行也是空行,也要读的
String temp="";
i=sis.readLine(oneLine,0,oneLine.length);
while(i!=-1)
{
temp=newLine(oneLine,0,i,charEncode);
if (temp.indexOf(getBoundary())>=0)
break;
fieldValue=fieldValue+temp;
i=sis.readLine(oneLine,0,oneLine.length);
}
//加入哈希表中
dic.put(fieldName,fieldValue);
fieldValue="";
}
}
i=sis.readLine(oneLine,0,oneLine.length);
}//end while
sis.close();
return true;
} //end doUpload
//////////////////////////
//清空Hashtable
public void clearDic() {
dic.clear();
if (dic.isEmpty()) {
System.out.println("empty");
}
else {
System.out.println("not empty");
}
}
//////////////////////////////////
//测试用的主函数
public static void main(String args[])
{
String[] fileList=null;
try{
FileUploadBean fub=new FileUploadBean();
fileList=fub.listFiles("/avatars/");
for(int i=0;i<fileList.length;i++)
System.out.println(fileList[i]);
}catch(Exception e){ e.printStackTrace();}
}
}
///////////////////////////////////
////文件目录过滤内部类
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页
jsp技巧大全 结束。