downloadfileaction.java

来自「SSH示范」· Java 代码 · 共 49 行

JAVA
49
字号
package com.iplan.portal.order.action;

import java.io.OutputStream;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;

import com.iplan.portal.framework.base.struts.BaseAction;

/**
 * http://www.hao-se.cn
 * 
 * @author ws
 */
public class DownloadFileAction extends BaseAction {

	public ActionForward download(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		DynaActionForm daf = (DynaActionForm) actionForm;
		FormFile formFile = (FormFile) daf.get("formFile");
		String fileName = (String) daf.get("fileName");
		
		fileName = "D:\\oceansoft\\Tomcat 5.0\\webapps\\iplan\\excle\\order.xls";

		formFile.setFileName(fileName);
		
		try {
			OutputStream os = httpServletResponse.getOutputStream();
			
			httpServletResponse.setContentType("application/x-msdownload");
			httpServletResponse.setHeader("Content-Disposition", "attachment;"
					+ " filename="
					+ new String(fileName.getBytes(), "ISO-8859-1"));
			os.write(formFile.getFileData());
			os.close();
		} catch (Exception e) {

		}
		return actionMapping.findForward("Excle");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?