public class ExportExcelInfo extends BaseAction {
private String filename;
@SuppressWarnings("unused")
private static final long serialVersionUID = 1L;
private static final LogDebug logDebug = new LogDebug(ExportExcelInfo.class);
public String getfilename() {
return filename;
}
public void setfilename(String filename) {
this.filename = filename;
}
public String execute() throws Exception{
this.getResponse().setContentType("application/vnd.ms-excel");
this.getResponse().setHeader("Content-disposition", "attachment;filename=a.xls");
this.filename="\\template\\a.xls";
String filePath=this.getRequest().getRealPath(this.filename);
byte[] buffer = new byte[16*1024];
int read;
File file = new File(filePath);
InputStream is = new FileInputStream(file);
OutputStream os = this.getResponse().getOutputStream();
while((read = is.read(buffer))>=0){
System.out.println("read:"+read);
os.write(buffer,0,read);
os.flush();
}
os.close();
is.close();
return null;
}
}
千万注意,在上面的输出流,必须用 this.getResponse.getOutPutStream才行,不然将无法把服务器端的文件下载到本地
private String filename;
@SuppressWarnings("unused")
private static final long serialVersionUID = 1L;
private static final LogDebug logDebug = new LogDebug(ExportExcelInfo.class);
public String getfilename() {
return filename;
}
public void setfilename(String filename) {
this.filename = filename;
}
public String execute() throws Exception{
this.getResponse().setContentType("application/vnd.ms-excel");
this.getResponse().setHeader("Content-disposition", "attachment;filename=a.xls");
this.filename="\\template\\a.xls";
String filePath=this.getRequest().getRealPath(this.filename);
byte[] buffer = new byte[16*1024];
int read;
File file = new File(filePath);
InputStream is = new FileInputStream(file);
OutputStream os = this.getResponse().getOutputStream();
while((read = is.read(buffer))>=0){
System.out.println("read:"+read);
os.write(buffer,0,read);
os.flush();
}
os.close();
is.close();
return null;
}
}
千万注意,在上面的输出流,必须用 this.getResponse.getOutPutStream才行,不然将无法把服务器端的文件下载到本地
版权声明:本文为Scorpius8928原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。