写道
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.paradise.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts Creation date: 06-06-2010
*
* XDoclet definition:
*
* @struts.action validate="true"
*/
public class GetAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UnsupportedEncodingException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException {
{
String aFilePath = null; // 要下载的文件路径
String aFileName = null; // 要下载的文件名
FileInputStream in = null; // 输入流
ServletOutputStream out = null; // 输出流
try {
aFilePath = request.getSession().getServletContext()
.getRealPath("/")
+ File.separator + "file" + File.separator;
aFileName = request.getParameter("f");
if (aFileName.endsWith("html")){
response.setContentType("text/html;charset=utf-8");
}else if (aFileName.endsWith("pdf")){
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "filename="+aFileName);
} else if (aFileName.endsWith("doc")){
response.setContentType( "application/msword");
response.setHeader( "Content-disposition ", "inline;filename="+aFileName);
}else if (aFileName.endsWith("bmp") || aFileName.endsWith("gif") || aFileName.endsWith("jpeg") || aFileName.endsWith("jpg") || aFileName.endsWith("png")){
// 禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
}else{
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition",
"attachment;filename=" + aFileName);
}
in = new FileInputStream(aFilePath + aFileName); // 读入文件
out = response.getOutputStream();
out.flush();
int aRead = 0;
while ((aRead = in.read()) != -1 & in != null) {
out.write(aRead);
}
out.flush();
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
return null;
}
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.paradise.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts Creation date: 06-06-2010
*
* XDoclet definition:
*
* @struts.action validate="true"
*/
public class GetAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UnsupportedEncodingException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException {
{
String aFilePath = null; // 要下载的文件路径
String aFileName = null; // 要下载的文件名
FileInputStream in = null; // 输入流
ServletOutputStream out = null; // 输出流
try {
aFilePath = request.getSession().getServletContext()
.getRealPath("/")
+ File.separator + "file" + File.separator;
aFileName = request.getParameter("f");
if (aFileName.endsWith("html")){
response.setContentType("text/html;charset=utf-8");
}else if (aFileName.endsWith("pdf")){
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "filename="+aFileName);
} else if (aFileName.endsWith("doc")){
response.setContentType( "application/msword");
response.setHeader( "Content-disposition ", "inline;filename="+aFileName);
}else if (aFileName.endsWith("bmp") || aFileName.endsWith("gif") || aFileName.endsWith("jpeg") || aFileName.endsWith("jpg") || aFileName.endsWith("png")){
// 禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
}else{
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition",
"attachment;filename=" + aFileName);
}
in = new FileInputStream(aFilePath + aFileName); // 读入文件
out = response.getOutputStream();
out.flush();
int aRead = 0;
while ((aRead = in.read()) != -1 & in != null) {
out.write(aRead);
}
out.flush();
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
return null;
}
}
}