zoukankan      html  css  js  c++  java
  • struts2——文件下载(简单的功能)

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@taglib uri="/struts-tags" prefix="s"%>
     4 <!DOCTYPE html>
     5 <html>
     6     <head>
     7         <meta charset="UTF-8">
     8         <title>struts2的一个例子</title>
     9     </head>
    10     <body>
    11         <s:form action="downloadFile.action" method="post" enctype="multipart/form-data">
    12             <s:submit value="下载文件"/>
    13         </s:form>
    14         
    15     </body>
    16 </html>
    index.jsp代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6     <constant name="struts.devMode" value="true"/>
     7     <package name="hello" extends="struts-default" namespace="/">
     8         <action name="downloadFile" class="com.xiaostudy.web.DownloadFile" method="downloadFile">
     9             <result name="success" type="stream">
    10                 <param name="inputName">fileInputStream</param>
    11                 <param name="contentDisposition">attachment;filename=image.jpg</param>
    12                 <param name="contentType">application/octet-stream</param>
    13             </result>
    14         </action>
    15     </package>
    16 </struts>
    struts.xml代码
     1 package com.xiaostudy.web;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.InputStream;
     7 
     8 import org.apache.struts2.ServletActionContext;
     9 
    10 import com.opensymphony.xwork2.ActionSupport;
    11 
    12 public class DownloadFile extends ActionSupport {
    13     
    14     public InputStream fileInputStream;
    15     public String downloadFile() throws FileNotFoundException {
    16         
    17         String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files/CSS.jpg");
    18         fileInputStream = new FileInputStream(new File(path));
    19         
    20         return SUCCESS;
    21     }
    22     public InputStream getFileInputStream() {
    23         return fileInputStream;
    24     }
    25     public void setFileInputStream(InputStream fileInputStream) {
    26         this.fileInputStream = fileInputStream;
    27     }
    28 
    29 
    30 }
    action动作类DownloadFile

  • 相关阅读:
    Java判断一个字符是数字或字母
    java数组和字符串相互转换
    java 字符串截取的三种方法
    Templates && Algorithms
    挖坑——未完成题目列表QwQ
    作业_2018.08.25
    BZOJ1008 [HNOI2008]越狱 (快速幂,组合)
    UR #3 核聚变反应强度( gcd )
    A Super Hero
    NOIP2015 pj
  • 原文地址:https://www.cnblogs.com/xiaostudy/p/9452227.html
Copyright © 2011-2022 走看看