BlogJava 首页 新随笔 联系 聚合 管理
  37 Posts :: 64 Stories :: 21 Comments :: 0 Trackbacks
/**
     * 实现文件另存功能
     * 
     * 
@param text
     *            文件内容
     * 
@param fileName
     *            文件名称
     * 
@return
     
*/

    
protected String renderFile(String text, String fileName)
            
throws IOException
    
{
        response.addHeader(
"Content-Disposition""attachment; filename="
                
+ fileName);
        response.setContentType(
"application/octet-stream");
        response.setCharacterEncoding(
"GB2312");
        response.getWriter().write(text);
        response.flushBuffer();
        response.getWriter().close();
        
return null;
    }

下载的action:
/**
     * 提供下载的方法
     * 
@return
     
*/

    
public String down()
    
{
        String dir 
= getFullPath() + "/upload/file/";
        
try
        
{
            
if (!FileUtils.exists(dir))
            
{
                
new File(dir).mkdirs();
            }

            Random r 
= new Random(System.currentTimeMillis());
            Integer randomInt 
= r.nextInt();
            
this.renderFile("test content:" + randomInt,randomInt + ".txt");
        }

        
catch (IOException e)
        
{
            e.printStackTrace();
            
this.renderText(e.getMessage());
        }

        
return null;
    }


posted on 2009-08-07 23:37 xiachang88 阅读(165) 评论(0)  编辑  收藏

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


网站导航: