使用JspSmartUpload组件上传文件

上传页面
<% ... @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< body >
< form  name ="form1"  enctype ="multipart/form-data"  method ="post"  action ="file2.jsp" >
  上传文件1:
< input  type ="file"  name ="file1"  size ="20"  maxlength ="20" >< br >
  文件描述1:
< input  type ="text"  name ="file1"  size ="20"  maxlength ="20" >< br >< br >
  上传文件2:
< input  type ="file"  name ="file2"  size ="20"  maxlength ="20" >< br >
  文件描述2:
< input  type ="text"  name ="file2"  size ="20"  maxlength ="20" >< br >< br >
  上传文件3:
< input  type ="file"  name ="file3"  size ="20"  maxlength ="20" >< br >
  文件描述3:
< input  type ="text"  name ="file3"  size ="20"  maxlength ="20" >< br >< br >
  
< input  type ="submit"  value ="submit" />
  
< input  type ="reset"  value ="reset" />
</ form >
</ body >
</ html >

 

上传处理页面

 

<% ... @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
< jsp:directive .page import ="javax.servlet.jsp.tagext.TryCatchFinally" />

<% ... @ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"  %>
< jsp:useBean  id ="mysmartupload"  class ="com.jspsmart.upload.SmartUpload" />

< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< body >
<% ...
    
int count=0//计算文件上传个数
    mysmartupload.initialize(pageContext);  
//初始化
    mysmartupload.setMaxFileSize(
1024*1024*5);
    mysmartupload.setAllowedFilesList(
"htm,txt");
    mysmartupload.setAllowedFilesList(
"htm,html,txt");//设置允许上传类型
    
    
String saveDirectory="/upload"//设置上传目录
    
String contextPath=request.getSession().getServletContext().getRealPath(saveDirectory);
    
    try{
         mysmartupload.upload();
         mysmartupload.save(contextPath);
    }catch(Exception e){
        out.println(
"file type error!");
    }
    
 
   
 
%>
</ body >
</ html >

 

如何查看上传文件的详细信息呢,如下代码可以做到

for ( int  i = 0 ;i < mysmartupload.getFiles().getCount();i ++ ) ... {
       com.jspsmart.upload.File myfile
=mysmartupload.getFiles().getFile(i);
       
if(!myfile.isMissing())...{
          
//将文件保存在指定路径
          
          out.println(
"FieldName="+myfile.getFieldName()+"<br>");
          out.println(
"Size="+myfile.getSize()+"<br>");
          out.println(
"FileName="+myfile.getFileName()+"<br>");
          out.println(
"FieldExc="+myfile.getFileExt()+"<br>");
          out.println(
"FilePathName="+myfile.getFilePathName()+"<br>");
          out.println(
"FileContextType="+myfile.getContentType()+"<br>");
          out.println(
"ContentDisp="+myfile.getContentDisp()+"<br>");
          out.println(
"TypeMIME="+myfile.getTypeMIME()+"<br>");
          out.println(
"SubTypeMime="+myfile.getSubTypeMIME()+"<br>");
          count
++;
       }

       
       out.println(
"<br>"+mysmartupload.getFiles().getCount()+ "files could be uploaded");
       out.println(count
+" files has been uploaded");
       
    }


你可能感兴趣的:(html,jsp,servlet,sun)