文件上传

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-INF\chenggong.jsp</result>
        </action></package></struts>    

 

转载于:https://www.cnblogs.com/laohan110/p/3525558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值