struts2上载
struts2下载
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.springframework.beans.factory.annotation.Autowired; import com.dhhc.cs.oa.service.common.AttachmentManager; import com.dhhc.cs.oa.web.action.BaseAction; /** * @category 下载附件 Action * @version 1.0 * @author yoyang * @company eqcms * @Date 2010-9-29 */ @SuppressWarnings("serial") @Results({ @Result(name = "success", type = "stream", params = { "contentType", "application/octet-stream", "inputName", "inputStream", "contentDisposition", "attachment;filename=\"${fileName}\"" }) }) public class DownloadAction extends BaseAction { @Autowired private AttachmentManager attachmentManager;; private Long id; public String execute() throws Exception { return SUCCESS; } public InputStream getInputStream() throws Exception { FileOutputStream out = null; try { out = new FileOutputStream(getTmpDir() + "/" + getFileName()); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } return new FileInputStream(getTmpDir() + "/" + getFileName()); } public String getFileName() { return attachmentManager.get(id).getName(); } public String getTmpDir() { String uploadDir = ServletActionContext.getServletContext() .getRealPath("/resources/tmp"); File dirPath = new File(uploadDir); if (!dirPath.exists()) { dirPath.mkdirs(); } return uploadDir; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
使用struts2标准的方式,返回页面一个输出流