使用Oreilly上传组件上传文件攻略

系统 1426 0

上传页面

<% ... @ 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 ="file.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 "
%>
<% ... @ page import = " java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest "   %>
<% ... @ taglib prefix = " fmt "  uri = " http://java.sun.com/jsp/jstl/fmt "   %>
<! 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 >
< fmt:requestEncoding  value ="gb2312" />
< body >
<% ...
   
String  saveDirectory = " /upload " // 设置上传目录
   
String  contextPath = request.getSession().getServletContext().getRealPath(saveDirectory);
   System.out.println(contextPath);
   File a
= new  File(contextPath);
   System.out.println(a.isDirectory());
   
if (!a.isDirectory()){
      a.mkdir();
   }
   
int  maxPostSize = 5 * 1024 * 1024 // 设定大小为5M
   
String   filename = null ;
   
String   contentType = null ;
   
String   description = null ;
   
int  count = 0 ;
   MultipartRequest multi
= new  MultipartRequest(request,contextPath,maxPostSize);
   
   
   
// 取得所有上传文件输入类型名称及描述
   Enumeration filesname
= multi.getFileNames();
   Enumeration filesdesc
= multi.getParameterNames();
   
while (filesname.hasMoreElements()){
     
String  name = ( String )filesname.nextElement();
     
String  dc = ( String )filesdesc.nextElement();
     filename
= multi.getFilesystemName(name);
     contentType
= multi.getContentType(name);
     description
= multi.getParameter(dc);
     
if (filename! = null ){
       count
++ ;
       
%>
         您上传第
<% = count  %> 个文件: < br >
         文件名:
<% = filename  %> < br >
         文件类型:
<% = contentType  %> < br >
         文件描述: 
<% = description  %> < br >< br >
       
<% ...
     }
   }
 
%>
 
您总共上传
<% = count  %> 个文件
 
</ body >
</ html >

 

 

需要注意两点:

(1)上传目录必须存在

(2)以上代码暂时不支持中文,支持中文只需要改动
          String enCoding="gb2312";
          MultipartRequest multi=new MultipartRequest(request,contextPath,maxPostSize,enCoding);
    



使用Oreilly上传组件上传文件攻略


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论