博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2单文件上传
阅读量:4596 次
发布时间:2019-06-09

本文共 3941 字,大约阅读时间需要 13 分钟。

第一步:首先写个上传文件的页面(简单的一个form表单)

文件上传

文件上传

文件上传1:

第二步:创建一个基类BaseAction.java,继承ActionSupport,并实现ServletRequestAware,ServletResponseAware,ServletContextAware三个接口,重写三个接口的set方法

public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{    /**     * 编写一个基类,继承ActionSupport并实现相应的接口     * 以后的Action直接继承该类,就可以简单获取到Servlet API     * 这是一个典型的适配设计模式     * @author Owen     */    private static final long serialVersionUID = 7267018575222346353L;            @Override    public void setServletContext(ServletContext servletContext) {    }    @Override    public void setServletResponse(HttpServletResponse response) {    }    @Override    public void setServletRequest(HttpServletRequest request) {    }}

第三步:创建OneUploadAction请求处理类,继承BaseAction

public class OneUploadAction extends BaseAction {    private static final long serialVersionUID = -4445894434193884175L;//    该属性名必须和中name值一致    private File oneFile;//    真实名称    private String oneFileFileName;//    文件类型    private String oneFileContentType;        private HttpServletRequest request;        @Override    public void setServletRequest(HttpServletRequest request) {        this.request = request;    }    @Override    public String execute() throws Exception {//        获取保存上传文件在服务器的真是路径        String uploadPath = request.getServletContext().getRealPath("upload");        System.out.println(uploadPath);//目录真是路径        System.out.println("oneFile--"+oneFile.getName());//文件临时名称        System.out.println("oneFileFileName--"+oneFileFileName);//文件原始名称        System.out.println("oneFileContentType--"+oneFileContentType);//文件类型//        第一种:该步骤适合上传小文件//        将临时文件复制到硬盘上的真是路径        /*        File file = new File(uploadPath, oneFileFileName);//拼接文件的存放路径和存放文件的真实名称        FileUtils.copyFile(oneFile, file);//将临时文件复制到上面这个路径        */        //        第二种:适合大文件上传操作        /*InputStream is = null;        OutputStream os = null;                is = new FileInputStream(oneFile);        os = new FileOutputStream(new File(uploadPath,oneFileFileName));                byte[] buffer = new byte[500];        int length = 0;        while((length=is.read(buffer,0,buffer.length)) != -1){            os.write(buffer, 0,length);        }        os.close();        is.close();*/        copyFile(uploadPath);                return SUCCESS;    }    public void copyFile(String uploadPath){        InputStream is = null;        OutputStream os = null;                try {            is = new FileInputStream(oneFile);            os = new FileOutputStream(new File(uploadPath,oneFileFileName));                        byte[] buffer = new byte[500];            int len = 0;            while((len=is.read(buffer,0,buffer.length)) != -1){                os.write(buffer, 0,len);            }            os.close();            is.close();        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }    public File getOneFile() {        return oneFile;    }    public void setOneFile(File oneFile) {        this.oneFile = oneFile;    }    public String getOneFileFileName() {        return oneFileFileName;    }    public void setOneFileFileName(String oneFileFileName) {        this.oneFileFileName = oneFileFileName;    }    public String getOneFileContentType() {        return oneFileContentType;    }    public void setOneFileContentType(String oneFileContentType) {        this.oneFileContentType = oneFileContentType;    }    }

第三步:配置struts.xml文件

/WEB-INF/jsp/one_upload_ok.jsp

第四步:浏览器测试

转载于:https://www.cnblogs.com/xujianbo/p/4918531.html

你可能感兴趣的文章
VirtualBox 下 CentOS6.5 网络配置(NAT) (公司内部用)
查看>>
面向对象三大基本特性,五大基本原则
查看>>
User32.dll 函数的相关方法整理
查看>>
Bootstarp 水平垂直居中
查看>>
LInux Centos7 重装yum
查看>>
HDU 3802Ipad,IPhone
查看>>
高德坐标转百度坐标并导航
查看>>
mpvue配合less
查看>>
查询一个表的不同数据,显示到一个表
查看>>
Unity随手记
查看>>
Swagger入门教程
查看>>
nginx日志定时切割脚本
查看>>
HashSet的实现原理
查看>>
oracle创建简单的包
查看>>
[POJ2689]Prime Distance
查看>>
python连接zookeeper的日志问题
查看>>
pycharm上传代码到码云错误现象用户密码
查看>>
柔性数组-读《深度探索C++对象模型》有感
查看>>
rmdir 命令(转)
查看>>
生产者消费者
查看>>