【struts2】struts2中的流接收与流发送

【前言】在我们的struts2后端中,实现流的接收和发送。就能够实现向server传视频流以及下载图片。

【流接收】

如今举一个传公钥的样例。struts2用一个action接收Key,而Key就是用http二进制流传过来的。

配置文件struts.xml写起来非常easy:

<action name="key" class="com.seeplant.action.PublicKeyAction" method="key">

  <result>/WEB-INF/content/SUCCESS.jsp</result>

  <result>/WEB-INF/content/ERROR.jsp</result>

</action>

仅仅是简单的指定了用Public String key()方法来处理这个Action,Action中用Servlet的Request读出流

public class PublicKeyAction extends ActionSupport {

  public String key() {

    try (ObjectInputStream oStream = new ObjectInputStream(ServletActionContext.getRequest().getInputStream());) {

       java.security.Key publicKey = (Key) oStream.readObject();

        System.out.println("公钥 "+publicKey);

    } catch (Exception e) { e.printStackTrace();}

  }

}

为了配合測试,贴上client发请求的代码。

public class MyTest {
    private final static int KEYSIZE = 512;
    private final static String kurlstring = "http://127.0.0.1/key";
    public static void main(String[] args) {
        try {
            URL url = new URL(kurlstring);

            URLConnection uConnection = url.openConnection();
            uConnection.setRequestProperty("content-type", "application/octet-stream"); //注意这里的content-type,一定要按规范写
            uConnection.setDoOutput(true);
            OutputStream oStream = uConnection.getOutputStream();

            // 生成Key
            KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance("RSA");
            SecureRandom random = new SecureRandom();
            pairGenerator.initialize(KEYSIZE,random);
            KeyPair keyPair = pairGenerator.generateKeyPair();

            try (ObjectOutputStream out = new ObjectOutputStream(oStream);){
                out.writeObject(keyPair.getPublic());
                out.flush();
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            uConnection.getInputStream();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

【流发送】流发送一般用于server公布流媒体。像中文名的图片就能够用流发送的方式提供,而不用改动tomcat的配置

struts.xml中,相应的流发送Action要配置成stream模式

<action name="download" class="com.seeplant.action.AndroidAppDownload">

    <param name="inputPath">/WEB-INF/androidVersion/SeePlant.apk</param> <!-- 这里指定了源文件的路径,在action类中有一个inputPath属性-->

    <result type="stream">

        <!--这里要指定content Type-->

        <param name="contentType">application/vnd.android.package-archive</param>

        <!--这个inputName一定要有,他是框架默认的入口函数标示,这里定义了一个targetFile,structs在收到client请求的download.action之后,会定位到getTargetFile函数。细致体会一下这里的相应关系-->

        <param name="inputName">targetFile</param>

        <!-- 这里是设置client收到的文件名称 ,client将会看到将要打开一个叫做SeePlantPack.apk的文件 -->

        <param name="contentDisposition">filename="SeePlantPack.apk"</param>

        <param name="bufferSize">4096</param>

    </result>


Action非常easy

public class AndroidAppDownload extends ActionSupport {

    private String inputPath;

    public void setInputPath(String inputPath) { this.inputPath = inputPath;}

    // 注意这里的入口方法的返回值是InputStream接口类型

    public InputStream getTargetFile() throws Exception {

        return ServletActionContext.getServletContext().getResourceAsStream(inputPath);

    }

}

原文地址:https://www.cnblogs.com/gavanwanggw/p/6915012.html