zoukankan      html  css  js  c++  java
  • struts2-上传文件

    package web;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.IOUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UploadDemoAction extends ActionSupport {
        private String username;
        private File myfile;
        private String myfileContentType;
        private String myfileFileName;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public File getMyfile() {
            return myfile;
        }
        public void setMyfile(File myfile) {
            this.myfile = myfile;
        }
        public String getMyfileContentType() {
            return myfileContentType;
        }
        public void setMyfileContentType(String myfileContentType) {
            this.myfileContentType = myfileContentType;
        }
        public String getMyfileFileName() {
            return myfileFileName;
        }
        public void setMyfileFileName(String myfileFileName) {
            this.myfileFileName = myfileFileName;
        }
        public String upload(){
            try {
                String path = ServletActionContext.getServletContext().getRealPath(
                        "/upload/file");
                System.out.println("path========"+path);
                InputStream input = new FileInputStream(myfile);
                System.out.println("myfileFileName===="+myfileFileName);
                String allpath=path+"\"+myfileFileName;
                System.out.println("allpath===="+allpath);
                OutputStream output = new FileOutputStream(allpath);
                IOUtils.copy(input, output);
                return SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return INPUT;
            }
        }
    }
    <%@ 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="upload.action" method="post" enctype="multipart/form-data">
        用户名:<input type="text" name="username"/>
        文件:<input type="file" name="myfile" />
        <input type="submit" value="提交"/>
    </form>
    </body>
    </html>
    <?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>
        <!-- 忽略struts格式 -->
        <constant name="struts.ui.theme" value="simple"></constant>
        <constant name="struts.configuration.xml.reload" value="true"></constant>
        <package name="houseuser" extends="struts-default" namespace="/">
        
        
        
        <!-- 上传图片示例-->
            <action name="upload" class="web.UploadDemoAction" method="upload">
                <result >success.jsp</result>
            </action>
        </package>
        
    </struts>    
  • 相关阅读:
    JS-BOM操作-Location、history、常用弹窗、屏幕属性
    JS的基础DOM操作-选取父子级元素、动态生成元素、修改元素、Classlist
    setup
    循环请求接口,统一处理
    多个url文件下载
    扁平数据结构转Tree
    es6 解构赋值
    watch与computed与props
    v-model与.sync组件通信
    v-on="$listeners"和v-bind="$attrs"
  • 原文地址:https://www.cnblogs.com/sincoolvip/p/6106515.html
Copyright © 2011-2022 走看看