posts - 5,  comments - 7,  trackbacks - 0

 

public String btnSaveXML_action() {
        
// TODO: Process the action. Return value is a navigation
        
// case name where null will return to the same page.
        try {
            
//get the appId
            Integer appId = Integer.valueOf(txtMemory1.getText().toString());
            String appName 
= datamanagementservice.getAppNameByAppId(appId);
            Document doc 
= writeXML(appId, appName);
            TransformerFactory tFactory 
= TransformerFactory.newInstance();
            Transformer transformer 
= tFactory.newTransformer();
            DOMSource source 
= new DOMSource(doc);

            
//the file's loadpath
            String loadPath = "c:\\temp\\" + appName + ".xml";
            
//if the file no exist then create it
            File f = new File("c:\\temp\\");
            f.mkdirs();
            StreamResult result 
= new StreamResult(new java.io.File(loadPath));
            transformer.transform(source, result);

            
//new a download file
            File t_file = new File(loadPath);
            
long l;
            
for (l = 0L; l == 0L; l = t_file.length()) {

                t_file 
= new File(loadPath);
            }

            InputStream in 
= new FileInputStream(t_file);
            
if (in != null{
                String filename 
= t_file.getName();
                filename 
= java.net.URLEncoder.encode(filename, "utf-8");

                FacesContext.getCurrentInstance().responseComplete();
                HttpServletResponse httpServletResponse 
= (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                httpServletResponse.reset();
                httpServletResponse.setContentType(
"application/x-msdownload");
                String rrr 
= "attachment; filename=" + filename;
                httpServletResponse.setHeader(
"Content-Disposition", rrr);
                httpServletResponse.setContentLength((
int) l);

                
byte b[] = new byte[2048];
                
for (int len = 0; (len = in.read(b)) > 0;) {
                    httpServletResponse.getOutputStream().write(b, 
0, len);
                }

                in.close();
            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }

        getRequestBean1().setSelectAppId((Integer) dropDown1.getSelected());

        
return null;
    }
posted on 2008-11-27 10:39 Vincent-chen 阅读(238) 评论(0)  编辑  收藏 所属分类: JSF

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


网站导航: