我想下载我正在创建的MS Word 2003文档,其中包含内容myString
。 我用下面的代码:无法使用HttpServletResponse下载文件
BufferedReader reader = new BufferedReader(new FileReader(templatePathFilename));
HttpServletResponse response = new MyHttpServletResponse();
response.setContentType ("application/msword");
response.setHeader ("Content-Disposition", "attachment; filename=\""+outgoingFileName);
ServletOutputStream myOut = response.getOutputStream();
myOut.write(myString.getBytes());
myOut.flush();
myOut.close();
而行myOut.write(myString.getBytes())
给出了一个NullPointerException。 MyHttpServletResponse
是eclipse默认生成的类,当我快速修复生成的错误时。我需要修改那个班吗? 任何人都可以请帮忙!
编辑:实际的代码我的工作是如下:
BufferedReader reader = new BufferedReader(new FileReader(templatePathFilename));
String outgoingFileName = outputPathFilename;
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment; filename="+outgoingFileName);
OutputStream myOut = response.getOutputStream();
try {
String thisLine;
while ((thisLine = reader.readLine()) != null) {
if(thisLine.contains("##"))
{
for (java.util.Enumeration e = FrontSheetMap.keys(); e.hasMoreElements();{
String name = (String) e.nextElement();
String value = FrontSheetMap.get(name).toString();
thisLine= thisLine.replaceAll("##" + name.toUpperCase() + "##", value);
}
}
myOut.write(thisLine);
myOut.write("\n");
}
myOut.flush();
}catch(Exception e){}
while循环替换所需的值,在thisLine
新的内容要在输出写入输入文件中的占位符文件。我需要一个下载选项,点击执行此代码的链接时弹出。
myString在哪里被声明和定义?另外,你确定它不是那个值是空的吗? – jmort253 2012-04-10 05:43:42
是的,它不为空。即使我将'myOut.write(1234);'或'myOut.write(“hello”.getBytes());'或任何其他字符串直接写入该函数,该函数也不起作用。 – 2012-04-10 05:45:48
你的读者是否需要传递给某些东西?我看到你在第一行宣布了它,但是我没有看到你实际使用它的地方。我在很久以前没有完成File IO,但我假设你包含这个原因。 – jmort253 2012-04-10 05:47:24