文件上传下载—struts2实现

来源:互联网 发布:js 屏蔽鼠标中键 编辑:程序博客网 时间:2024/06/03 19:47

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>

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

文件下载未完待续。。。

1 0
原创粉丝点击