zoukankan      html  css  js  c++  java
  • 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;
    	}
    	
    }
    
  • 相关阅读:
    流量染色与gRPC服务托管 微服务协作开发、灰度发布之流量染色 灰度发布与流量染色
    http://www.cnblogs.com/sealedbook/p/6194047.html
    celery 原理
    修改织梦默认栏目页、文章页URL命名规则
    Dede首页幻灯版显示Bug修正
    DEDECMS5.7 首页和栏目页调用文章按权重排序
    dede文章摘要字数的设置方法
    DEDECMS登录后台慢的完美解决方案
    DedeCMS去掉友情链接中“织梦链投放”“织梦链”的方法
    删除dedecms5.7后台登陆验证码
  • 原文地址:https://www.cnblogs.com/jiangwenwen1/p/9465729.html
Copyright © 2011-2022 走看看