posts - 193,  comments - 520,  trackbacks - 0

commons.fileupload实现文件的上传,代码如下:
<%!
 //服务器端保存上传文件的路径
    String saveDirectory = "g:\\upload\\";
    // 临时路径 一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
    String tmpDirectory = "g:\\upload\\tmp\\";
    // 最多只允许在内存中存储的数据大小,单位:字节
    int maxPostSize = 1024 * 1024;
%>
<%
    // 文件内容 
    String FileDescription = null;
    // 文件名(包括路径)
    String FileName = null;
    // 文件大小
    long FileSize = 0;
    // 文件类型
    String ContentType = null;

%>

<%
   DiskFileUpload fu = new DiskFileUpload();
    // 设置允许用户上传文件大小,单位:字节
   fu.setSizeMax(200*1024*1024);
    // 设置最多只允许在内存中存储的数据,单位:字节
   fu.setSizeThreshold(maxPostSize);
    // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
   fu.setRepositoryPath("g:\\upload\\tmp\\");
    //开始读取上传信息 得到所有文件
   try{
      List fileItems = fu.parseRequest(request);
     }catch(FileUploadException e){
         //这里异常产生的原因可能是用户上传文件超过限制、不明类型的文件等
         //自己处理的代码
     }
%>
<%
   // 依次处理每个上传的文件
   Iterator iter = fileItems.iterator();
   while (iter.hasNext()) {
     FileItem item = (FileItem) iter.next();
       //忽略其他不是文件域的所有表单信息
     if (!item.isFormField()) {
       String name = item.getName();
       long size = item.getSize();
       String  contentType = item.getContentType();
     if((name==null||name.equals("")) && size==0)
       continue;
%>
<%
   //保存上传的文件到指定的目录
  String[] names=StringUtils.split(name,"\\");  //对原来带路径的文件名进行分割
   name = names[names.length-1];
   item.write(new File(saveDirectory+ name));
  }
}
%>
 下面是其简单的使用场景:
 A、上传项目只要足够小,就应该保留在内存里。
 B、较大的项目应该被写在硬盘的临时文件上。
 C、非常大的上传请求应该避免。
 D、限制项目在内存中所占的空间,限制最大的上传请求,并且设定临时文件的位置。
 
 可以根据具体使用用servlet来重写,具体参数配置可以统一放置到一配置文件
 



 文件的下载用servlet实现
      public void doGet(HttpServletRequest request,
                       HttpServletResponse response)
     {
         String aFilePath = null;    //要下载的文件路径
         String aFileName = null;    //要下载的文件名
         FileInputStream in = null;  //输入流
         ServletOutputStream out = null;  //输出流

         try
   {
          
             aFilePath = getFilePath(request);
             aFileName = getFileName(request);

             response.setContentType(getContentType(aFileName) + "; charset=UTF-8");
             response.setHeader("Content-disposition", "attachment; filename=" + aFileName);

             in = new  FileInputStream(aFilePath + aFileName); //读入文件
            out = response.getOutputStream();
            out.flush();
            int aRead = 0;
           while((aRead = in.read()) != -1 & in != null)
        {
             out.write(aRead);
         }
           out.flush();
      }
       catch(Throwable e)
     {
     log.error("FileDownload doGet() IO error!",e);
      }
         finally
         {
             try
             {
                 in.close();
                 out.close();
             }
             catch(Throwable e)
             {
              log.error("FileDownload doGet() IO close error!",e);
             }
         }
     }



http://www.blogjava.net/ronghao 荣浩原创,转载请注明出处:)
posted on 2005-12-16 10:46 ronghao 阅读(6221) 评论(1)  编辑  收藏 所属分类: j2se基础

FeedBack:
# re: 用commons.fileupload实现文件的上传和下载
2005-12-16 11:05 | wolfsquare
还是Webwork爽,声明即所得.
只需要在action里声明public setFiles(File[] files){...}即可直接使用上传到的文件,哪里用那么麻烦啊..  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2005年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

关注工作流和企业业务流程改进。现就职于ThoughtWorks。新浪微博:http://weibo.com/ronghao100

常用链接

留言簿(38)

随笔分类

随笔档案

文章分类

文章档案

常去的网站

搜索

  •  

最新评论

阅读排行榜

评论排行榜