`

struts 2 批量上传文件下载以及struts 2简介、操作

阅读更多

上传jsp

---------------------------------------------


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%@taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html> 

 <head>

    <base href="<%=basePath%>">

    

    <title></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">

<script type="text/javascript">  

function addMore()  

{  

    var td = document.getElementById("more");  

    var br = document.createElement("br");  

    var input = document.createElement("input");  

    var button = document.createElement("input");  

      

    input.type="file";  

    input.name="file";  

      

    button.type="button";  

    button.value="Remove";  

      

    button.onclick = function(){  

        td.removeChild(br);  

        td.removeChild(input);  

        td.removeChild(button);  

    }  

      

    td.appendChild(br);  

    td.appendChild(input);  

    td.appendChild(button);  

}  

</script> 

</head> 

  <body>  

        <form action="upload/save.do"  enctype="multipart/form-data" method="post">       

         <table align="center" width="40%">  

            <tr>

                <td>file:</td>  

                <td id="more"><!-- 定义一个id,方便javascript调用 -->                      

                    <input type="button" value="Add More..." onclick="addMore()">  

                </td>  

            </tr>  

            <tr>  

                <td><input type="submit" value="提交">  </td>  

            </tr>  

        </table>         

        </form>

  </body>  

</html>  

------------------------------------------------------------------------------------------------------------

http://yitong.xiaodoutao.com/


上传Action

-----------------------

public class UploadAction extends ActionSupport {



private List<File> file;  

    //下面两个变量是文件名与文件类型,Struts2会自动为以下两变量赋值  

    private List<String> fileFileName;  

    private List<String> fileContentType;  

 

    public List<File> getFile() {  

        return file;  

    }  

    public void setFile(List<File> file) {  

        this.file = file;  

    }  

    public List<String> getFileFileName() {  

        return fileFileName;  

    }  

    public void setFileFileName(List<String> fileFileName) {  

        this.fileFileName = fileFileName;  

    }  

    public List<String> getFileContentType() {  

        return fileContentType;  

    }  

    public void setFileContentType(List<String> fileContentType) {  

        this.fileContentType = fileContentType;  

    }





public String save() {

String root = ServletActionContext.getRequest().getRealPath("upload");

    File dir = new File(root);

    if (dir.exists()==false){

   

      dir.mkdir();

         }

   

    for(int i = 0;i < file.size(); i++){ 

       

            InputStream is = new FileInputStream(file.get(i));   

            File destFile = new File(root,this.getFileFileName().get(i));  

         

            OutputStream os = new FileOutputStream(destFile);  

            byte[] buffer = new byte[1024];  

            int length = 0;  

            while((length= is.read(buffer)) > 0){  

                os.write(buffer, 0, length);  

            }  

            is.close();  

            os.close();  


         }

return SUCCESS;

}


}

------------------------------------------------------------------------------------------------------------


上传struts.xml

----------------------------------------


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>


<package name="default" extends="authority">

<action name="upload/*" method="{2}" class="{1}Action">

<interceptor-ref name="upload">  

                配置允许上传的文件类型,多个用","分隔   

              <param name="allowedTypes">  

          image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg  

              </param>  

                配置允许上传的文件大小,单位字节   

              <param name="maximumSize">102400</param>  

           </interceptor-ref>  

          <interceptor-ref name="defaultStack" /> 

          <result name="success">/success.jsp</result>

</struts>

------------------------------------------------------------------------------------------------------------



下载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 'down.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">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

  </head> 

  <body>

    测试检验test <br>

    <a href="downloadCenter/execute.do?fileName=${fileName}" >下载中心</a>

  </body>

</html>

--------------------------------------------------------------------------------------------------------------------------------


下载action

----------------------------------------------------------------------------

public class DownloadCenterAction extends ActionSupport {




String inputName = "inputStream";

String fileName;

public String getFileName() {

//从页面获取文件名

fileName = getRequest().getParameter("fileName");

String downFileName =fileName ;

   try { 

     //中文文件名也是需要转码为 ISO8859-1,否则乱码

    downFileName =  new String(fileName.getBytes("ISO8859-1"), "UTF-8"); 

   } catch (UnsupportedEncodingException e) { 

     e.printStackTrace();

   } 

   return downFileName;

}

public InputStream getDownloadFile(){  

    try {

    InputStream in = ServletActionContext.getServletContext().getResourceAsStream("upload/"+fileName );

    System.err.println(in);

    return in;      

} catch (Exception e) {

return null;

}   

}

public String execute() throws Exception {  

    //调用相关业务逻辑方法,动态设置相关下载信息   

       //contentType = "application/octet-stream;charset=ISO8859-1";

        return SUCCESS;  

    }  


}

------------------------------------------------------------------------------------------------------------

下载struts.xml


--------------------------------------------------------


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>

     <!--下载中心 -->

<package name="download_center" extends="authority">

<action name="*/*" method="{2}" class="{1}Action">

<result name="success" type="stream">

<!-- 设置内容类型 -->

<param name="contentType">application/octet-stream;charset=ISO8859-1</param>

<!-- 设置下载文件的名字  attachment:作为附件,filename=:指定下载文件名-->

<param name="contentDisposition">attachment;filename="${downFileName}"</param>

<!-- 设置下载文件的输入流对应的方法 downloadFile对应DownloadAction中的getDownloadFile()-->

<!--对应的getDownloadFile类中的getDownloadFile方法-->

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

<!-- 指定下载文件的缓冲大小 -->

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

</result>

</action>

</package>


</struts>

------------------------------------------------------------------------------------------------------------

http://yitong.xiaodoutao.com/

 

1
0
分享到:
评论
2 楼 miaowei 2011-12-07  
何以见得,而且我测试过的阿,最后将数据保存到数据库了,保存到数据库的一些代码没有贴出来........................
1 楼 jyjava 2011-12-06  
如果你批量的搞绝对不行

相关推荐

Global site tag (gtag.js) - Google Analytics