ych

struts文件上传

http://blog.csdn.net/techyang/archive/2005/08/09/448677.aspx(luanfengxia/arc/2006/05/20/746951.aspx)

 public ActionForward add(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {
        String encoding = request.getCharacterEncoding();
        if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
        {
            response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
        }
        NodeForm theForm = (NodeForm) form;
        String nodename=theForm.getNodename();
        String note=theForm.getNote();
        FormFile file = theForm.getTheFile1();//取得上传的文件
        FormFile file2=theForm.getTheFile2();
        FormFile file3=theForm.getTheFile3();
        Session session=null;
        String forward="";
        try
        {
            /*
             * 取当前系统路径
             */
            String filePath = this.getServlet().getServletContext()
            .getRealPath("/");
            System.out.println("---------------------------------------"+filePath);
            if(file.getFileSize()!=0){
            ByteArrayInputStream stream = (ByteArrayInputStream) file.getInputStream();//把文件读入
          
            //ByteArrayOutputStream baos = new ByteArrayOutputStream();
          
            /*
             * 建立一个上传文件的输出流
             */
            OutputStream bos = new FileOutputStream(filePath +
                    "UploadFiles\\"+file.getFileName());
            request.setAttribute("fileName",filePath + "/"
                    + file.getFileName());
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
            {
                bos.write(buffer, 0, bytesRead);//将文件写入服务器
            }          
            bos.close();
            stream.close();
            }if(file2.getFileSize()!=0){
            ByteArrayInputStream stream2 = (ByteArrayInputStream) file2.getInputStream();
            OutputStream bos2 =  new FileOutputStream(filePath +
                    "UploadFiles\\"+file2.getFileName());//建立一个上传文件的输出流
            int bytesRead2 = 0;
            byte[] buffer2 = new byte[8192];
            while ((bytesRead2 = stream2.read(buffer2, 0, 8192)) != -1)
            {
                bos2.write(buffer2, 0, bytesRead2);//将文件写入服务器
            }          
            bos2.close();
            stream2.close();
            }if(file3.getFileSize()!=0){
            ByteArrayInputStream stream3 = (ByteArrayInputStream) file3.getInputStream();//把文件读入
        OutputStream bos3 =  new FileOutputStream(filePath +
                "UploadFiles\\"+file3.getFileName());//建立一个上传文件的输出流
        int bytesRead3 = 0;
        byte[] buffer3 = new byte[8192];

        while ((bytesRead3 = stream3.read(buffer3, 0, 8192)) != -1)
        {
            bos3.write(buffer3, 0, bytesRead3);//将文件写入服务器
        }          
        bos3.close();
        stream3.close();
        }
  Configuration config = new Configuration().configure();
  SessionFactory factory = config.buildSessionFactory();
     session = factory.openSession();
        Transaction transaction = session.beginTransaction();
        Node node=new Node();
        node.setNodeName(nodename);
        node.setXsdName(file.getFileName());
        node.setXslName(file2.getFileName());
        node.setXhtmlName(file3.getFileName());
        node.setNote(note);
        session.save(node);
  session.flush();
  session.clear();
  forward="display";
        transaction.commit();
        }
        catch (Exception e)
        {
         forward="error";
            System.err.print(e);
            e.printStackTrace();
        }
   
  finally{
   session.close();
   }
        return mapping.findForward(forward);
 }

posted on 2007-08-29 13:35 changhong 阅读(174) 评论(0)  编辑  收藏


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


网站导航:
 

导航

<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(1)

随笔档案

文章档案

相册

搜索

最新评论

阅读排行榜

评论排行榜