当前位置: 移动技术网 > IT编程>开发语言>Java > struts2+jsp实现文件上传的方法

struts2+jsp实现文件上传的方法

2017年12月08日  | 移动技术网IT编程  | 我要评论

本文实例讲述了struts2+jsp实现文件上传的方法。分享给大家供大家参考。具体如下:

1. java代码:

package com.wang.test;
import java.io.inputstream;
import org.apache.struts2.servletactioncontext;
import com.opensymphony.xwork2.action;
import com.opensymphony.xwork2.actionsupport;
public class downloadphonefile extends actionsupport
{
  ////getdownloadfile()方法返回的必须是inputstream。getresourceasstream()方法可以通过流的方式将资源输出 
  public inputstream getdownloadfile()
  {
    return servletactioncontext.getservletcontext().getresourceasstream("/upload/userlogin_7.27.apk");
  }
  public string execute()
  {
    return action.success;
  }
  /*************【struts2的文件下载的实现方式】*********************************************/ 
  //如果直接写一个链接链到所要下载的文件上的话,对于有的时候,默认的会自动在浏览器里面打开 
  //这种情况非常不利于我们的文件下载和权限控制。因此,我们实现文件下载时都不会采用这种方式 
  //我们所采用的是标准http协议的方式,输出二进制的流,导致浏览器认识这个流,它再进行文件下载 
  //实际上这种方式是跟输出有关的,当点击下载链接时,会产生下载的一个信息。它是跟result有关的 
  //所以就到struts-default.xml中查看<result-type/>结果类型 
  //其中<result-type name="stream" class="org.apache.struts2.dispatcher.streamresult"/> 
  //事实上,这里的streamresult类是专门用来执行文件下载的 
  /*************【每一次下载文件时,控制台都会提示socket异常】***********************************/ 
  //报错信息为java.net.socketexception:connection reset by peer: socket write error 
  //下载本身也是socket操作,于是抛出该异常。实际上这个异常可以忽略掉。每次下载的时候,都会抛出该异常 
  //在getdownloadfile()方法上throws exception之后,控制台上就不会再报告这个异常信息啦 
  /*************【用于处理文件下载的streamresult类的源代码片段】********************************/ 
  //这里显示的是org.apache.struts2.dispatcher.streamresult类的源代码片段 
  //public class streamresult extends strutsresultsupport{ 
    //protected string contenttype = "text/plain"; 
    //protected string contentlength; 
    //protected string contentdisposition = "inline"; 
    //protected string inputname = "inputstream"; 
    //protected inputstream inputstream; 
    //protected int buffersize = 1024; 
  /*************【浅析streamresult类的三个重要属性】******************************************/ 
  //这里我们主要关注一下streamresult类的三个属性:contenttype、contentdisposition、inputname 
  //这些属性都是通过在struts.xml配置之后,由struts2自动注入到对象里面去的 
  //其中contenttype用来指定下载的文件的类型,contentdisposition用来指定下载文件的名字 
  //另外buffersize用来设定下载文件时的缓冲区大小,默认为1kb,通常按照默认的1kb就可以了 
  //实际上这些属性完全是根据http协议得来的。http协议就规定了下载文件的时候,需要使用到这些属性 
  //其中最关键的就是protected string inputname属性,它是用来指定真正下载的文件的io流 
  //因此downloadaction中必须返回一个输入流。因为下载的时候,本身就是一个从服务器端将文件输入过来的操作 
  /***************************************************************************************/ 
}

2. xml代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <package name="default" extends="struts-default" namespace="/">
    <action name="download" class="com.wang.test.downloadphonefile">
      <result name="success" type="stream">
        <param name="contenttype">application/vnd.ms-powerpoint</param>
        <param name="contentdisposition">attachment;filename="userlogin_7.27.apk"</param>
        <param name="inputname">downloadfile</param>
      </result>
    </action>
  </package>
</struts>

3. jsp页面代码如下:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>">
  <title>my jsp 'index.jsp' starting page</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="this is my page">
 </head>
 <body>
  <input type="button" value="手机端安装包下载" onclick="javascript:window.location='download.action';"/> 
 </body>
</html>

希望本文所述对大家的jsp程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网