• Struts2之下载


    下载页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="download.action?FileName=Java.pdf">下载</a>
    </body>
    </html>
    

    struts.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    <!-- 开启开发者模式 -->
    <constant name="struts.devMode" value="true"></constant>
    <package name="jiangwenwen" namespace="/" extends="struts-default">
    	<action name="download" class="cn.jiangwenwen.action.DownLoadAction" method="download">
    		<result name="success" type="stream">
    			<param name="contentDisposition">
    				attachment;filename="${fileName}"
    			</param>
    			<param name="contentLength">
    				${fileSize}
    			</param>
    			<param name="contentType">
    				${contentType}
    			</param>
    			
    		</result>
    	</action>
    </package>
    </struts>
    

    Action类

    public class DownLoadAction extends ActionSupport{
    	
    	private String contentType;
    	
    	private String FileName;
    	
    	private InputStream inputStream;
    	
    	private long fileSize;
    	
    	public String download() throws FileNotFoundException {
    		
    		
    		String rootpath = ServletActionContext.getServletContext().getRealPath("/");
    		
    		System.out.println(rootpath);
    		
    		System.out.println(FileName);
    		
    		File file = new File(rootpath+"/download/"+FileName);
    		
    		fileSize = file.length();
    		
    		inputStream = new FileInputStream(file);
    		
    		return SUCCESS;
    		
    		
    	
    	}
    
    	public String getContentType() {
    		return contentType;
    	}
    
    	public void setContentType(String contentType) {
    		this.contentType = contentType;
    	}
    
    	public String getFileName() {
    		return FileName;
    	}
    
    	public void setFileName(String fileName) {
    		FileName = fileName;
    	}
    
    
    
    	public InputStream getInputStream() {
    		return inputStream;
    	}
    
    	public void setInputStream(InputStream inputStream) {
    		this.inputStream = inputStream;
    	}
    
    	public long getFileSize() {
    		return fileSize;
    	}
    
    	public void setFileSize(long fileSize) {
    		this.fileSize = fileSize;
    	}
    	
    }
    
  • 相关阅读:
    PostgreSQL学习的九层宝塔
    MySQL和PostgreSQL在多表连接算法上的差异
    覆盖equals 时总要覆盖hashCode(9)
    覆盖equals 时总要覆盖hashCode(9)
    覆盖equals 时总要覆盖hashCode(9)
    覆盖equals 时总要覆盖hashCode(9)
    SQL Server 字段和对应的说明操作(SQL Server 2005 +)
    SQL Server 字段和对应的说明操作(SQL Server 2005 +)
    SQL Server 字段和对应的说明操作(SQL Server 2005 +)
    SQL Server 字段和对应的说明操作(SQL Server 2005 +)
  • 原文地址:https://www.cnblogs.com/jiangwenwen1/p/9465729.html
Copyright © 2020-2023  润新知