动作struts与文件响应不使用ajax

时间:2012-09-04 14:31:02

标签: jquery ajax file struts

我有一个struts动作,它有一个doc作为响应,这是代码:

public class GenerateBlame extends Action
{ 



    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("it's ok!");
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\decisionRetraitSanction.doc"));
        HWPFDocument doc = new HWPFDocument(fs);
        Range r = doc.getRange();
        r.replaceText("<dateDemande>", "test");

        doc.write(new FileOutputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\decision.doc"));
        OutputStream out = response.getOutputStream();
        response.setContentType("application/rtf");
        response.setHeader("Content-Disposition","attachment; filename=Decision");
        FileInputStream in = new FileInputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\decision.doc");
        byte[] buffer = new byte[4096];
        int length;
    /*  while ((length = in.read(buffer)) > 0){
            out.write(buffer, 0, length);
        }
        in.close();
        out.flush();*/
        return null;
    }
}

我想通过ajax id调用此操作,如下所示:

$.ajax({
  type: "POST",
  url: "../generateBlame.do",
 });

我无法将文件下载,因为我想要一个表格

1 个答案:

答案 0 :(得分:1)

您需要使用this jQuery download plugin或类似内容。