upload

File saveDir=new File("D:/Test/UPLOAD");
        if(!saveDir.isDirectory()){
            saveDir.mkdirs();
        }
        File repository = (File) getServletConfig().getServletContext().getAttribute("javax.servlet.context.tempdir");
        DiskFileItemFactory factory = new DiskFileItemFactory(10*1024*1024, repository);

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(10*1024*1024);
        // Parse the reques*
        try {
            List<FileItem> items = upload.parseRequest(request);
            Iterator<FileItem> iter=items.iterator();
            while (iter.hasNext()) {
                FileItem item = iter.next();
                if(!item.isFormField()){
                    String filename=item.getName();
                    System.out.println(filename);
                    item.write(new File(saveDir,filename));
                    upload_msg="上传成功!";
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

 

posted @ 2016-04-25 11:01  maozs  阅读(425)  评论(0编辑  收藏  举报