Jessei
11/29/2016 - 9:46 AM

download

download

package com.haiyisoft.upload.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.haiyisoft.entity.common.UploadTable;
import com.haiyisoft.ep.common.jpa.util.JPAUtil;
import com.haiyisoft.ep.common.model.QueryParamList;
import com.haiyisoft.ep.framework.model.DataWrap;
import com.haiyisoft.ep.framework.ui.struts2.action.BaseAction;

/**
 * 
 * @ClassName: ZsgcDownFileShowAction
 * @Description: 显示下载文件
 * 
 */
@Namespace("/kfqb/common")
@ParentPackage("framework-default")
@Action(value = "/downFileShowAction", results = { @Result(name = "input", location = "uploadFileShowList.jsp") })
public class DownFileShowAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unused")
	private transient DataWrap<UploadTable> dataWrap = new DataWrap<UploadTable>();

	public String download() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String appCode = request.getParameter("dataWrap.query.appCode");
		QueryParamList qpl = new QueryParamList();// 获取从前台传递过来的参数
		qpl.addParam("appCode", appCode);
		List<UploadTable> docList = JPAUtil.load(UploadTable.class, qpl);
		for (UploadTable doc : docList) {
			byte[] docAfix = doc.getAffix();
			InputStream inputstream = new ByteArrayInputStream(docAfix);
			ServletOutputStream outfile = null;
			try {

				if (inputstream != null) {
					response.reset();
					response.addHeader("Content-Disposition", "attachment;filename="
							+ new String(doc.getFileName().getBytes("GB2312"), "ISO8859-1"));
					if (doc.getFileName() != null && doc.getType() != null) {
						if ("application/vnd.openxmlformats-officedocument.wordprocessingml.document".equals(doc
								.getType())) {
							response.setContentType("application/msword");
						} else {
							response.setContentType(doc.getType());
						}

					}
				}

				outfile = response.getOutputStream();
				int len;
				while ((len = inputstream.read(docAfix)) > 0) {
					outfile.write(docAfix, 0, len);
					outfile.flush();
				}

			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					outfile.close();
					inputstream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		}
		return null;
	}

	@Override
	protected void initTreat() {
		this.retrieveAfterInit = true;
	}

	@Override
	public String init() throws Exception {
		return null;
	}

	@Override
	public DataWrap<UploadTable> getDataWrap() {
		return null;
	}

	public void setDataWrap(DataWrap<UploadTable> dataWrap) {
		this.dataWrap = dataWrap;
	}
}