zoukankan      html  css  js  c++  java
  • Struts2上传

    用到的包:一个是commons-fileupload-1.3.2.jar,另一个是commons-io-2.2.jar,没有的话去struts-2.3.31appsWEB-INFlib里面拷。

    第一步:做界面

    1.在webcontent里面建一个包,这里叫Upload,然后新建两个jsp,input是界面,upload是成功跳转的界面

    在input.jsp中的form表单,除了写action和method,还需要加上enctype="multipart/form-data"

    在选择文件的<input>标签中的type要写file

    在upload中写一句话就可以了,仅仅作为一个页面的标记,不写也可以。这里写个“上传成功”

    第二步:做action

    package com.itnba.maya.controller;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Random;
    import org.apache.commons.io.FileUtils;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UploadAction extends ActionSupport{
        private String titile;
        private File file;
        private String fileFileName;//文件名
        private String fileContentType;//文件类型
        
        public String getFileFileName() {
            return fileFileName;
        }
        public void setFileFileName(String fileFileName) {
            this.fileFileName = fileFileName;
        }
        public String getTitile() {
            return titile;
        }
        public void setTitile(String titile) {
            this.titile = titile;
        }
        public File getFile() {
            return file;
        }
        public void setFile(File file) {
            this.file = file;
        }
        public String input(){
            return SUCCESS;
        }
        public String upload() throws IOException{
            //随机数
            Random rand = new Random();
            //给随机数一个范围
            int n = rand.nextInt(9999);
            //格式化一下,不够位补0
            DecimalFormat df = new DecimalFormat("0000");
            //把n放到format中格式化
            String s = df.format(n);
            
            //日期格式化
            SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddhhmmss");
            //获取当前时间
            Date now = Calendar.getInstance().getTime();
            //把当前时间格式化
            String prefix = sim.format(now);
            
            //把随机数、时间和文件名拼接作为复制后的文件名
            String newname = prefix+"_"+s+"_"+fileFileName;
            //复制的地址
            File destFile = new File("d://"+newname);
            //执行
            FileUtils.copyFile(file, destFile);
            return SUCCESS;
        }
        
    }

    插入的代码是我们的action的内容,执行一下看下结果。

    上传一个叫新建文本文档.txt,上传成功后我们去D盘找一下

    D盘有了一个加了时间和随机数的新建文本文档

     ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    -------------------------------------------------------------------------------------------------

    三个成员:1.File类型 2.String fileFileName属性名 3.String fileContentType属性名
    org.apache.commons.io.FileUtils.copyFile很简单。否则用IO流也可以操作。
    用IO流操作
    InputStream is = new FileInputStream(file); 
    OutputStream os = new FileOutputStream(new File(root, fileFileName));

    byte[] buffer = new byte[500];
    int length = 0;

    while(-1 != (length = is.read(buffer, 0, buffer.length)))
    {
    os.write(buffer);
    }

    os.close();
    is.close();

    -------------------------------------------------------------------------------------------------

    1.如何解决文件重名的问题?
    加时间,随机数,用户名,序列号
    2.如何解决大文件上传的问题?
    在struts.xml 中添加常量。这个常量在struts.properties中(/org/apache/struts2/default.properties)。
    <constant name="struts.multipart.maxSize" value="20971520"></constant>

  • 相关阅读:
    如何在ASP.NET 5和XUnit.NET中进行LocalDB集成测试
    如何在单元测试过程中模拟日期和时间
    Azure Blob Storage从入门到精通
    免费电子书:使用VS Online敏捷管理开源项目
    使用ASP.NET 5开发AngularJS应用
    Syncfusion的社区许可及免费电子书和白皮书
    理解ASP.NET 5的中间件
    惊鸿一瞥(Glimpse)——开发之时即可掌控ASP.NET应用的性能
    用于Simple.Data的ASP.NET Identity Provider
    大数据技术之_19_Spark学习_01_Spark 基础解析小结(无图片)
  • 原文地址:https://www.cnblogs.com/liyh123/p/6589004.html
Copyright © 2011-2022 走看看