【struts2】文件上传下载—struts2实现

struts2实现上传下载时要用到的jar包:

这里写图片描述

UploadAction.java(fileupload.action)

package com.rczp.action;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    private File image; //上传的文件
    private String imageFileName; //文件名称
    private String imageContentType; //文件类型
    private String message;//显示上传情况

    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public File getImage() {
        return image;
    }
    public void setImage(File image) {
        this.image = image;
    }
    public String getImageFileName() {
        return imageFileName;
    }
    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }
    public String getImageContentType() {
        return imageContentType;
    }
    public void setImageContentType(String imageContentType) {
        this.imageContentType = imageContentType;
    }

    public String execute() throws Exception {
        System.out.println("上传图片的execute方法。。。。。。。。。。。。。。。。");
        String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        //D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
        System.out.println("realpath: "+realpath);
        if (image != null) {
            File savefile = new File(new File(realpath), imageFileName);
            if (!savefile.getParentFile().exists())
                savefile.getParentFile().mkdirs();
            FileUtils.copyFile(image, savefile);
            message="文件上传成功";

            return "result";
        }else {
            return "input";
        }
    }

upload.jsp(在该页面进行文件上传)

%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="struts" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>upload page</title>
  </head>

  <body>
        <struts:form action="fileupload" enctype="multipart/form-data">
                <struts:file type="file" name="image"></struts:file>
                <struts:submit label="提交"></struts:submit>
        </struts:form>
         <br/>
        <struts:fielderror />
    </body>

</html>

uploadResult.jsp(在该页面显示上传结果)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="struts" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>uploadResult page</title>
  </head>

  <body>
        ${ message }
    <br/><br/>
   文件路径:<struts:property value="'images/'+imageFileName"/>
    <struts:debug></struts:debug>

  </body>
</html>

struts.xml中的配置

<struts>
    <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->
    <constant name="struts.multipart.maxSize" value="10701096"/>
<!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir -->
    <constant name="struts.multipart.saveDir " value="E:/zzmp" />

    <package name="main" extends="struts-default" >
        <action name="fileupload" class="com.rczp.action.UploadAction" >
            <result name="result">/uploadResult.jsp</result>
            <result name="input">/upload.jsp</result>
           <!-- 动态设置savePath的属性值 -->
           <param name="savePath">/images</param>
         </action>
      </package>
</struts>

=======分割线==以上为文件上传==============

文件下载未完待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值