Java在线打开Word

http://bbs.csdn.net/topics/390245142


jsp页面


window.onload = function() {
        
            document.getElementById("divwrapped").innerHTML = "<iframe width='100%' height='100%' src='ajax_pageload.action?path=" + encodeURIComponent(filepath) + "' />";    };

然后在后台:(只写关键代码哦)

response.setHeader("Content-type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
// 设置下载头信息
try {
    downLoad(response, strUrl);
    response.flushBuffer();
} catch (Exception e) {
    e.printStackTrace();
 }
out.flush();
out.close();


downLoad ()方法


  /**
     * 下载文件
     * @param config
     * @param response
     * @param downLoadFileName
     */
    public static void downLoad(HttpServletResponse response,String filePath){
        BufferedInputStream bis=null;
        BufferedOutputStream  bos=null;
        try{
             String filename=filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
             response.setContentType("application/x-msdownload");
             response.setHeader("Content-Disposition","filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
             bis =new BufferedInputStream(new FileInputStream(filePath));
             bos=new BufferedOutputStream(response.getOutputStream());
             byte[] buff = new byte[2048];
             int bytesread;
             while(-1 != (bytesread = bis.read(buff, 0, buff.length))) {
              bos.write(buff,0,bytesread);
             }
        }catch(Exception e){
             e.printStackTrace();
        }finally {
         if (bis != null)
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
         if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
      
    }






你可能感兴趣的:(java)