• 文件上传


    jsp;

      <body>
        <form action="${pageContext.request.contextPath}/Shang.action" method="post"  enctype="multipart/form-data">
            <input type="file" name="myphoto">
            <input type="submit" value="上传">
        
        </form>
      </body>

    action;

        // 封装上传文件域的属性
        private File image;
        // 封装上传文件类型的属性
        private String imageContentType;
        // 封装上传文件名的属性
        private String imageFileName; //
        // 接受依赖注入的属性
        private String savePath;
    package com.file;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class CopyOfShangAction extends ActionSupport {
        private File myphoto;
        private String myphotoFileName;
    
    
    
        public File getMyphoto() {
            return myphoto;
        }
    
        public void setMyphoto(File myphoto) {
            this.myphoto = myphoto;
        }
    
        public String getMyphotoFileName() {
            return myphotoFileName;
        }
    
        public void setMyphotoFileName(String myphotoFileName) {
            this.myphotoFileName = myphotoFileName;
        }
    
        public String execute() throws Exception {
            FileOutputStream fos = null;
            FileInputStream fis = null;
            try {
                // 建立文件输出流
            
                fos = new FileOutputStream("D:/" + "\"
                        + getMyphotoFileName());
                // 建立文件上传流
                fis = new FileInputStream(getMyphoto());
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
            } catch (Exception e) {
                System.out.println("文件上传失败");
                e.printStackTrace();
            } finally {
                close(fos, fis);
            }
            System.out.println("ok");
            return SUCCESS;
        }
    
        private void close(FileOutputStream fos, FileInputStream fis) {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                    System.out.println("FileInputStream关闭失败");
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    System.out.println("FileOutputStream关闭失败");
                    e.printStackTrace();
                }
            }
        }
    
    }

    struts.xml

    <?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="shang" namespace="/" extends="struts-default">
            <action name="Shang" class="com.file.CopyOfShangAction">
                <result name="success">/WEB-INFchenggong.jsp</result>
            </action></package></struts>    
  • 相关阅读:
    8.24Java入门--->第二十六节
    IDEA-->右键没有创建包选项--->新建包不能自动扩展
    8.21Java入门--->第二十五节(多线程)
    个人冲刺(一)——体温上报app(二阶段)
    个人作业——家庭记账本
    个人冲刺(五)——家庭记账本
    个人冲刺(四)——家庭记账本
    个人冲刺(三)——家庭记账本
    个人冲刺(二)——家庭记账本
    个人冲刺(一)——家庭记账本
  • 原文地址:https://www.cnblogs.com/laohan110/p/3525558.html
Copyright © 2020-2023  润新知