chinasd211 2010-05-13 11:27
浏览 255
已采纳

jsp word文档处理

jsp构造出work文档之后,怎样通过一个Action把work文档保存,并且我不显示work文档

  • 写回答

4条回答 默认 最新

  • 蜉蝣撼大树 2010-05-14 15:22
    关注

    你既然都可以通过“jsp生成的word文档”那么你读取word里面的数据应该没什么难度了啊,你保存是下载到本地吗?你想要操作word的话给你推荐个jacob(开源的第三方工具包),你要是需要下载的话,我下面给你贴出来一个通过serlvet下载文件的类,配置在tomcat的web.xml后直接调用可以下载:

    [color=orange]
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import utils.ReadSavePathImpl;
    import inter.util.IReaderPath;

    /**

    • 文档下载
    • @author Administrator * */

    public class FileDowndoadServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            doPost(req, resp);
    }
    
    @SuppressWarnings("deprecation")
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            resp.setContentType("application/x-msdownload;charset=gb2312");
            //取得文件名
            String fileName = req.getParameter("fileName").trim();
            if(null==fileName){
                resp.sendRedirect(req.getContextPath()+"/error/error.jsp");
                return;
            }
            //p("希望下载的文件名:"+fileName);
            fileName = new String(fileName.getBytes("iso-8859-1"),"gb2312");
            //p("希望下载的文件名:"+fileName);
            //取得文件保存的路径
            IReaderPath readerPath = new ReadSavePathImpl();
            @SuppressWarnings("all")
            String filePath =  req.getRealPath(readerPath.getDirectoryName());
            resp.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"),"iso-8859-1"));
            InputStream is = null;
            OutputStream os = null;
            try{
                File file = new File(filePath,fileName);
                if(!file.exists()){ 
                    resp.sendRedirect(req.getContextPath()+"/error/error.jsp");
                    return;
                }
                FileInputStream fis = new FileInputStream(file);
                is= new BufferedInputStream(fis);
                os = resp.getOutputStream();
                byte[] buf = new byte[1024];
                int len = 0;
                while((len=is.read(buf,0,buf.length))>0){
                    os.write(buf,0,len);
                }
            }catch(Exception e){
                p("文档下载异常:"+e.getMessage());
                e.printStackTrace();
            }finally{
                try {
                    if (null != os) {
                        os.close();
                    }
                } catch (Exception e) {
                    p("关闭输出流错误:" + e.getMessage());
                }
                try {
                    if (null != is) {
                        is.close();
                    }
                } catch (Exception e) {
                    p("关闭输入流错误:" + e.getMessage());
                }
        }
    }
    private void p(Object obj){
        System.out.println("[ FileDowndoadAction   ]"+obj);
    }
    

    }
    [/color]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器