lizongbo 的编程学习

编程学习 java为主的web应用开发

  DonewsBlog  |  Donews首页  |  Donews社区  |  Donews邮箱  |  我的首页  |  联系作者  |  聚合   |  登录
  75篇文章 :: 0篇收藏:: 35篇评论:: 2个Trackbacks

公告

lizongbo的blog


文章

收藏

相册

常用网址

存档


正在读取评论……


曾经见过有人给出的最蠢的一个做法,是在ActionForm里放了20个Formfile 类型的变量。
其实只需要下面一段代码即可。

        String dirPath = getServlet().getServletContext().getRealPath("/")
                         + "/upload";
        Hashtable fileh = actionForm.getMultipartRequestHandler().
                          getFileElements();
        for (Enumeration e = fileh.keys(); e.hasMoreElements(); ) {
            String key = (String) e.nextElement();
            try {
                FormFile formfile = (FormFile) fileh.get(key);
                String filename = formfile.getFileName().trim(); //文件名
                if (!"".equals(filename)) {
  //不同的浏览器传上的文件名可能有区别,有的是全路径的
//在这里保存文件
                    InputStream ins = formfile.getInputStream();
                    OutputStream os = new FileOutputStream(dirPath +
                            File.separatorChar
                            + filename);
                    int bytesRead = 0;
                    byte[] buffer = new byte[8192];
                    while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                        os.write(buffer, 0, bytesRead);
                    }
                    os.close();
                    ins.close();

                }
            } catch (Exception ex) {
                logger.debug("出错了", ex);
            }
        }



Trackback: http://tb.donews.net/TrackBack.aspx?PostId=561262


[点击此处收藏本文]  发表于2005年09月21日 3:19 PM




正在读取评论……

发表评论

大名:
网址:
验证码
评论