当前位置: 代码迷 >> Java Web开发 >> jspsmartupload.jar上传文件,怎么限制上传文件的大小,如果超出限制,怎么弹出提示
  详细解决方案

jspsmartupload.jar上传文件,怎么限制上传文件的大小,如果超出限制,怎么弹出提示

热度:731   发布时间:2016-04-17 14:01:03.0
jspsmartupload.jar上传文件,如何限制上传文件的大小,如果超出限制,如何弹出提示?
以下是我的代码,如何加入上面的功能?


<%@   page   contentType= "text/html;charset=gb2312 "   language= "java "%>
<%@   page   import= "com.jspsmart.upload.*,java.sql.* "%>
<jsp:useBean   id= "mySmartUpload "   scope= "page "   class= "com.jspsmart.upload.SmartUpload "/>
<html>
    <head> <title> 上载附件 </title> </head>
    <body>
       
        <%
        String   userid   =   session.getAttribute( "username ").toString();
            try   {

                mySmartUpload.initialize(pageContext);

                mySmartUpload.service(request,response);

                mySmartUpload.upload();
                String   fn=mySmartUpload.getFiles().getFile(0).getFileName();
mySmartUpload.setAllowedFilesList( "xls ");
                com.jspsmart.upload.File   myFile   =   mySmartUpload.getFiles().getFile(0);
            myFile.saveAs( "E:/Tomcat   5.0/webapps/upload/ "+userid+ ".xls ");
             
            }catch(Exception   e)   {

          e.printStackTrace();}
        %>
       
   

    </body>
</html>



------解决方案--------------------
http://hmilyld.cn/read.php?465
blog上有个详细的例子.

SmartUpload su = new SmartUpload();
//初始化上传
su.initialize(pageContext);
//定义允许上传文件类型
su.setAllowedFilesList( "gif,jpg,rar,zip,doc,txt ");
//不允许上传文件类型
su.setDeniedFilesList( "jsp,asp,php,aspx,html.htm ");
//单个文件最大限制
su.setMaxFileSize(200000);
//总共上传文件限制
su.setTotalMaxFileSize(5000000);
//开始上传,如果超过限制或者为不允许类型,抛出异常提示
su.setContentDisposition(null);
try {
su.upload();
} catch (Exception e) {
out.println( "你选择的文件不允许上传或者文件过大,请返回检查! ");
}