使用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");
       
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值