zoukankan      html  css  js  c++  java
  • struts2上传多文件(b)

    UploadUtil.java<br>-------------------
    /**
     * 上传文件,名称以日期命名
     *
     * @author Administrator
     */
    public class UploadUtil {
        /**
         * 上传文件
         *
         * @param src
         *            源文件
         * @param dst
         *            目标文件
         * @param BUFFER_SIZE
         *            缓冲大小
         */
        public static void copy(File src, File dst, final int BUFFER_SIZE) {
            try {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = new BufferedInputStream(new FileInputStream(src));
                    dst = rename(dst);
                    out = new BufferedOutputStream(new FileOutputStream(dst));
                    int word = 0;
                    while ((word = in.read()) != -1) {
                        out.write(word);
                    }
                } finally {
                    if (null != in) {
                        in.close();
                    }
                    if (null != out) {
                        out.close();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        /**
         * 得到扩展名
         * @param fileName
         * @return
         */
        public static String getExt(String fileName) {
            int pos = fileName.lastIndexOf(".");
            return fileName.substring(pos);
        }
     
        /**
         * 更改上传文件名
         * @param file 文件对象
         * @return 更名后的文件对象
         */
        public static File rename(File file) {
            String fileName = "";// 文件名
            String extName = "";// 文件扩展名
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            fileName = formatter.format(cal.getTime()) + (int) (Math.random() * 10);
            extName = getExt(file.getName());
            String newName = fileName + "." + extName;
            file = new File(file.getParent(), newName);
            return file;
        }
     
    }

      

     upload.jsp
    <html>
      <body>
       <s:form method="post" action="jupload" enctype ="multipart/form-data">
        上传图片:<s:file name ="upload" theme="simple"/>
        <br/>
        上传附件:<s:file name ="upload" theme="simple"/>
        <br/>
        <s:submit theme="simple" value="提交"/>
      </s:form>
      </body>
    </html>

      struts.xml

    <!-- 上传文件 cyjch -->
    <action name="jupload"  method="add" class="upAction" >
        <result name="upload">upload.jsp</result>
    </action>
  • 相关阅读:
    【linux命令】命名管道(mkfifo)+ 结合xargs命令使用
    【SSH服务】远程连接管理服务SSH
    【VSFTP服务】rhel8安装vsftp软件
    【linux命令】关机和重启命令
    POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
    Windows下虚拟机安装Ubuntu15.10 Destop简易操作过程
    POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
    Ubuntu15.10下华南师大锐捷认证客户端的使用详解
    POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
    Ubuntu 14.04 LTS 下升级 gcc 到 gcc-4.9、gcc-5 版本
  • 原文地址:https://www.cnblogs.com/toge/p/6114660.html
Copyright © 2011-2022 走看看