Struts的文件上传和下载

Struts文件的上传功能与Struts本身并无太大关系,主要是依靠FileUtils来实现。而下载则是依靠Struts来实现的。

struts单个文件操作

package action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class StrutsFile {

private File file;
// 获取文件的名字 xxxFileName。与index.jsp中的input type="file"中的name属性一样
private String fileFileName;
// 获取文件的类型 xxxContentType。与index.jsp中的input type="file"中的name属性一样
private String fileContentType;
//下载文件的名字downName
private String downName;

//文件上传
public String upFile(){
    String path=ServletActionContext.getServletContext().getRealPath("/");
    try {
        FileUtils.copyFile(file, new File(path+"upFile/"+fileFileName));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "success";
}
//文件下载
public String down(){
    System.out.println("下载。。。。");
    return "index";
}
public InputStream getFileIO(){
    return ServletActionContext.getServletContext().getResourceAsStream("upFile/"+downName);
}

public File getFile() {
    return file;
}
public void setFile(File file) {
    this.file = file;
}
public String getFileFileName() {
    return fileFileName;
}
public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
}
public String getFileContentType() {
    return fileContentType;
}
public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
}
public String getDownName() {
    return downName;
}
public void setDownName(String downName) {
    this.downName = downName;
}

}

struts多个文件操作

package action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class StrutsFile2 {

private File[] file;
// 获取文件的名字 xxxFileName
private String[] fileFileName;
// 获取文件的类型 xxxContentType
private String[] fileContentType;

//文件上传
public String upFile(){
    String path=ServletActionContext.getServletContext().getRealPath("/");
    try {
        for (int i = 0; i < file.length; i++) {
            FileUtils.copyFile(file[i], new File(path+"upFile/"+fileFileName[i]));
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "success";
}

public File[] getFile() {
    return file;
}

public void setFile(File[] file) {
    this.file = file;
}

public String[] getFileFileName() {
    return fileFileName;
}

public void setFileFileName(String[] fileFileName) {
    this.fileFileName = fileFileName;
}

public String[] getFileContentType() {
    return fileContentType;
}

public void setFileContentType(String[] fileContentType) {
    this.fileContentType = fileContentType;
}

}

index.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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
   <br/> --------struts-----------
     <form action="strusUp!upFile" method="post" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="submit"/>
    </form>
    <br/>----struts 多文件-----
    <form id="ff" action="strusUp2!upFile" method="post" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="submit"/>
        <input onclick="show()" type="button" value="添加上传"/>
    </form>
    ------------------struts下载-----
    <br/>
     <a href="strusDown!down?downName=1.jpg">struts下载1.jpg</a>
    <script type="text/javascript">
        function show(){
            var form=document.getElementById("ff");
            var file=document.createElement("input");
            file.setAttribute("type","file");
            file.setAttribute("name","file");
            form.appendChild(file);
        }
    </script>
  </body>
</html>
Struts.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>
<!-- 设置上传的大小  默认是2M-->
<constant name="struts.multipart.maxSize" value="2097152*2"></constant>
    <package name="ss" namespace="/" extends="struts-default">
        <action name="myUp" class="action.MyUpFile">
            <result>index.jsp</result>
        </action>
        <!-- 文件上传 -->
        <action name="strusUp" class="action.StrutsFile">
            <result>index.jsp</result>
        </action>

        <action name="strusUp2" class="action.StrutsFile2">
            <result>index.jsp</result>
        </action>

        <!-- 文件下载 -->
        <action name="strusDown" class="action.StrutsFile">
            <result name="index" type="stream">
                <param name="contentType">application/x-msdownload</param>
                <!-- downName 得到页面传过来的名字 -->
                <param name="contentDisposition">attachment;filename=${downName}</param>
                <!-- 请把前面的get给干掉 -->
                <param name="inputName">fileIO</param>
            </result>
        </action>
    </package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值