在Action中写的保存成txt文件代码如下:
String filename = request.getParameter("filename");//"1a.txt";
String filepath = request.getParameter("filepath");//"d:\\";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename = "+filename);
FileWriter fw=null;
try { fw=new FileWriter(filepath+filename);
// Userword a=(Userword)all;
/* Iterator it = a.iterator();
while (it.hasNext()) {
fw.write(it.next().toString());
fw.write("\n");
}*/
int i;
for(i=0;i <all.size();i++){
Userword pm = (Userword)all.get(i);;
// System.out.println(pm.getUserword());
fw.write(pm.getUserword());
fw.write(pm.getUsertran());
fw.write("\n");
}
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (null != fw) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
页面的链接代码是:<a href="userword.do?status=printall&filepath=d:\\&filename=1a.txt" class="a1">文本导出 </a>
发现点击该链接后,能在盘符里自动保存正确的txt文件,但是在网页里提供的下载txt竟然是网页代码,这是怎么回事???
String filename = request.getParameter("filename");//"1a.txt";
String filepath = request.getParameter("filepath");//"d:\\";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename = "+filename);
FileWriter fw=null;
try { fw=new FileWriter(filepath+filename);
// Userword a=(Userword)all;
/* Iterator it = a.iterator();
while (it.hasNext()) {
fw.write(it.next().toString());
fw.write("\n");
}*/
int i;
for(i=0;i <all.size();i++){
Userword pm = (Userword)all.get(i);;
// System.out.println(pm.getUserword());
fw.write(pm.getUserword());
fw.write(pm.getUsertran());
fw.write("\n");
}
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (null != fw) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
页面的链接代码是:<a href="userword.do?status=printall&filepath=d:\\&filename=1a.txt" class="a1">文本导出 </a>
发现点击该链接后,能在盘符里自动保存正确的txt文件,但是在网页里提供的下载txt竟然是网页代码,这是怎么回事???
以前遇到过类似的问题。
struts2里用 HttpServletRequest req = ServletActionContext.getServletContext().getRequest();
它的ServletActionContext是重新封装过的,以前以类似LZ这种方式输出二进制流,接收方(C++写的控件)会报错。但改用原装的Servlet的response输出就正常。
你先试试用原装的Servlet试试有没有问题。