zoukankan      html  css  js  c++  java
  • smartUpload组件文件上传

    public class SmartUploadServlet extends HttpServlet {
    
        
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            doPost(request, response);
        }
    
        
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            String filePath = getServletContext().getRealPath("/")+"images";
            File file = new File(filePath);
            if(!file.exists()){
                file.mkdir();
            }
            
            SmartUpload su = new SmartUpload();
            //初始化;
            su.initialize(getServletConfig(), request, response);
            //设置上传文件大小
            su.setMaxFileSize(1024*1024*10);
            //设置所有的上传文件的大小
            su.setTotalMaxFileSize(1024*1024*100);
            //设置允许上传文件类型
            su.setAllowedFilesList("txt,jpg,gif");
            String result = "上传成功";
            try {
                su.upload();
                su.save(filePath);
            } catch (Exception e) {
                result = "上传失败";
                if(e.getMessage().indexOf("1015")!=-1){
                    result="上传失败:上传文件类项不正确";
                }else if(e.getMessage().indexOf("1010")!=-1){
                    result="上传失败:上传文件类项不正确";
                }
                else if(e.getMessage().indexOf("1105")!=-1){
                    result="上传失败:上传文件大小大于允许上传的最大值";
                }
                else if(e.getMessage().indexOf("1110")!=-1){
                    result="上传失败:上传的总的文件大小大于允许上传的最大值";
                }
            }
            request.setAttribute("result", result);
            request.getRequestDispatcher("jsp/02.jsp").forward(request, response);
        }
    
    }
  • 相关阅读:
    SecureCRT
    MySQL
    流式计算概述
    WEB加密算法
    drop、truncate和delete的区别
    JVM参数
    HTTP1.0与HTTP1.1区别
    HTTP请求方法
    Java查看JVM转态的命令总结
    go二次更新
  • 原文地址:https://www.cnblogs.com/james-roger/p/4981624.html
Copyright © 2011-2022 走看看