`
ldzyz007
  • 浏览: 715103 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

CommonUpload

    博客分类:
  • java
阅读更多
1.上传部分

   1)form的设置里,一定要设成:method="post" enctype="multipart/form-data"

   2)CommonUpload初始化设置:

           DiskFileItemFactory factory = new DiskFileItemFactory();
           factory.setSizeThreshold(1024);
                   // maximum size that will be stored in memory也就是允许传的最小的文件大小
           factory.setRepository(new File("d:\\0DPSdata\\TEMP"));
                   // the location for saving data that is larger than getSizeThreshold()设置临时目录

   2)request的要用以下方式实现:

           ServletFileUpload fu = new ServletFileUpload(factory);
           fu.setSizeMax(2*1024*1024);
           List fileItems = fu.parseRequest(request);
           Iterator iter = fileItems.iterator();

    3)循环提取部分:

           while(iter.hasNext()) {
                FileItem item = (FileItem)iter.next();
    
                if (item.isFormField()) {
                       ------则为一般传递的参数,用item.getFieldName()和item.getString()判断取值-------
                }

                if (!item.isFormField()) {
                       ------则为上传文件,用str=(FileInputStream)(item.getInputStream())放入输入流待处理-------
                }
           }

         4)存入数据库:

                主要用PreparedStatement实现二进制流入输入
                PreparedStatement pstmt=db.getSqlPreparedStatement(); //自己写的方法,此处略
                pstmt.setBinaryStream(1,str,str.available()); //1是指sql语句里面的第一个?
                pstmt.execute();   
                pstmt.close();
                str.close();

2.下载部分(jsp实现)

          1)初始设置:

                String filename="report.txt";    //下载文件名
                response.setContentType("text/plain");    //下载文件类型(关于响应报头格式,见下面备注)
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);

          2)循环取出:

                while(rs.next()){
                       ServletOutputStream sout=response.getOutputStream();
                      InputStream in=rs.getBinaryStream(1);  
                      byte b[]=new byte[1024];
                      int i = -1;
                      while ((i = in.read(b)) != -1) {  
                            sout.write(b,0,i);
                      }    
                      sout.flush();  
                      sout.close();  
                 }

3.响应报头格式(ContentType):




还有以下三个设置,暂时不知道啥意思,查查再说:

response.setHeader("Location",filename);
response.setHeader("Cache-Control", "max-age=" + cacheTime);
response.setContentLength(filelength);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics