码迷,mamicode.com
首页 > Web开发 > 详细

smartUpload组件文件上传

时间:2015-11-20 18:58:53      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }

}

 

smartUpload组件文件上传

标签:

原文地址:http://www.cnblogs.com/james-roger/p/4981624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!