DownloadAction實現下載示例


package com.cai.miniHr.struts.actions;

import java.io.File;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

public class ReportDownloadAction extends DownloadAction {

@Override
protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// Set the file name
arg3.setHeader("Content-disposition", "attachment; filename=" + "a.jpg");
String contentType = "image/gif";
// String contentType = "application/vnd.ms-excel";
// File file = new File("D:\\a.xls");
File file = new File(this.servlet.getServletContext().getRealPath("/") + "images\\a.jpg");
// System.out.println(this.servlet.getServletContext().getRealPath("/") + "images\\a.jpg");
return new FileStreamInfo(contentType, file);
}

}

this.servlet.getServletContext().getRealPath("/") + "images\\a.jpg"即爲F:\me\tomcat6\apache-tomcat-6.0.20\webapps\miniHr\images\a.jpg
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章