• 文件上传和下载


    文件上传

    文件上传的两个jar

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    Upload.jsp

    <%--文件上传--%>
    <form action="user/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="upload"/><br>
        <button type="submit">上传</button>
    </form>

    Struts.xml

    <!--文件上传-->
    <action name="upload" class="cn.xcq.entity.Action1" method="upload">
        <result>/success.jsp</result>
    </action>

     

     

    public class Action1 {

        private File upload;
        private String uploadFileName;
        private String uploadContentType;

     

    /*文件上传*/
    public String upload(){
     
        //文件上传路径
        String path="g:/FileUploader";
        String realPath=path+File.separatorChar+uploadFileName;

        try {
            FileUtils.copyFile(upload,new File(realPath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success";

    }

     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;
        }

        public String getUploadContentType() {
            return uploadContentType;
        }

        public void setUploadContentType(String uploadContentType) {
            this.uploadContentType = uploadContentType;
        }
    }

    Action中使用了三个属性封装文件信息

    File类型的XXX属性,与表单的File控件的name属性一样,用于封装File控件对应的文件内容

    String类型的xxxFileName属性,该属性名称由前面的File类型属性和FileName组合,是固定的语法,是封装File控件对应文件的文件名

    String类型的XXXContentType属性,同样由xxx属性和ContentType组合而成,是固定语法,封装File控件对应文件的文件类型

    文件下载

    Index.jsp

    <form action="user/download" method="post">
        <input type="text" name="inputPath">
        <button type="submit">下载</button>
    </form>

    Struts.xml

    <!--文件下载-->
    <action name="download" class="cn.xcq.entity.DownLoad" method="download">
        <result name="success" type="stream">
            <param name="contentDisposition">attachment;filename=${fileName}</param>
        </result>
    </action>

    DownLoad

    public class DownLoad {

        //读取下载文件的目录
        private String inputPath;

        //下载文件的文件名
        private String fileName;

        //读取下载文件的输入流
        private InputStream inputStream;

        //下载文件的类型
        private String conetntType;

        public  String download(){
            try {
                inputStream=new FileInputStream(inputPath);
                //获取文件明
                fileName= inputPath.substring(inputPath.lastIndexOf("\")+1);
                //解决中文乱码问题
                try {
                    fileName= URLEncoder.encode(fileName,"UTF-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return  "success";
        }



        public String getInputPath() {
            return inputPath;
        }

        public void setInputPath(String inputPath) {
            this.inputPath = inputPath;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public InputStream getInputStream() {
            return inputStream;
        }

        public void setInputStream(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public String getConetntType() {
            return conetntType;
        }

        public void setConetntType(String conetntType) {
            this.conetntType = conetntType;
        }
    }

    Name属性值要一致

  • 相关阅读:
    如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息
    STL的心得(4)运用(MFC)
    STL的心得(3)---运用(控制台)
    【Css】清除浮动,独占一行
    [Html]加链接提示
    【JS】清除子节点
    【CSS】使Div在父元素中水平居中
    [JS]回前页
    【JS】鼠标移动到链接上变手型
    【jQuery】改变控件的使能状态
  • 原文地址:https://www.cnblogs.com/xuchangqi1/p/9245632.html
Copyright © 2020-2023  润新知