• Struts2 简单的上传文件并且显示图片


    代码结构:

    UploadAction.java

    package com.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UploadAction extends ActionSupport {
        private File upload;
        private String uploadFileName;
        public File getUpload() {
            return upload;
        }
        public void setUpload(File upload) {
            this.upload = upload;
        }
        public String getUploadFileName() {
            return uploadFileName;
        }
        public void setUploadFileName(String uploadFileName) {
            this.uploadFileName = uploadFileName;
        }
        @Override
        public String execute() throws Exception {
            InputStream is = new FileInputStream(getUpload());
            String path = ServletActionContext.getServletContext().getRealPath("/upload");
            OutputStream os = new FileOutputStream(path+"/"+this.uploadFileName);
            byte buffer[] = new byte[1024];
            int count = 0;
            while((count=is.read(buffer))>0){
                os.write(buffer,0,count);
            }
            os.close();
            is.close();
            return SUCCESS;
        }
    }

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
          <head>
            <title>文件上传</title>
          </head>
          <body>
            <s:form action="myupload" method="post" enctype="multipart/form-data">
            <s:file name="upload" label="上传的文件"></s:file>
            <s:submit value="上传"></s:submit>
            </s:form>
          </body>
    </html>

    struts.jsp

    <?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>
        <package name="default" extends="struts-default">
            <action name="myupload" class="com.action.UploadAction">
                <result name="success">/success.jsp</result>
            </action>
        </package>
        <constant name="struts.multipart.saveDir" value="/tmp"></constant>
    </struts>    

    success.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
          <head>
            <title>文件上传</title>
          </head>
          
          <body>
            恭喜你,上传成功!<img src=upload/<s:property value="uploadFileName"/>>
          </body>
    </html>
  • 相关阅读:
    delete
    Database Files and Filegroups
    Getting ready for SQL Database
    Why are there directories called Local, LocalLow, and Roaming under Users<username>AppData?
    sql change automation
    What is the difference between POST and GET?
    Which side is more logical: front-end or back-end?
    The configuration element is not declared
    nlog配置文件的自动查找
    Serilog配置
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9963558.html
Copyright © 2020-2023  润新知