struts2文件上传简介

本文主要介绍struts2文件上传的简要操作步骤。目前我使用的版本是2.3.34。其实大致思路还是关于IO流的读写和struts1版本的实现思路是一样的,就是说先从页面中获得一个文件输入流,再根据输入流创建一个输出流,将文件写到一个文件夹中。具体步骤如下 :
首先我们先创建一个action代码如下:

package com.ev.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;

public class MyAction extends ActionSupport{
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;
    public File getUpload() {
        return upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadContentType() {
        return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public String getUploadFileName() {
        return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
    public String getSavePath() {
        savePath = ServletActionContext.getServletContext().getRealPath("\\upload");
        return savePath;
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
    @Override
    public String execute() throws Exception {

        File file = this.getUpload();
        String fileName = this.getUploadFileName();

        FileInputStream in = new FileInputStream(file);
        FileOutputStream out = new FileOutputStream(this.getSavePath()+"\\"+fileName);
        System.out.println(this.getSavePath()+fileName);
        byte[] arr = new byte[in.available()];
        in.read(arr);
        out.write(arr);

        in.close();
        out.close();
        return SUCCESS;
    }

}

这里需要注意的是upload成员与jsp页面中的文件域中的name属性值是一致的。这里的数据类型是java.io.File;这个在底层struts2已经帮我们封装好了,所以才能用这个类型,还有就是uploadContentTypet和uploadFileName成员分别是upload接ContentType和FileName;也就是说后面部分是固定不变的。savePath是文件上传后的保存路径。action后台代码写好后还要对其做一下配置如下(该配置写在struts.xml文件中):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/path" extends="struts-default">
        <action name="uploadAction" class="com.ev.action.MyAction">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

最后我们就可以在jsp页面中调用该action实现文件上传了,jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/struts2fileupload/path/uploadAction.action" method="post" enctype="multipart/form-data">
        <input type="file" name="upload" id="upload" /><br/>
        <input type="submit" value="上传">
    </form>
</body>
</html>

本次上传的文件位于upload目录下
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值