zoukankan      html  css  js  c++  java
  • 文件上传

    一:文件上传

    第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

    第二步:把form表的enctype设置为:“multipart/form-data“,如下:

    <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
      <input  type="file" name="uploadImage">
    </form>

    第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:

    public class HelloWorldAction{
      private File uploadImage;//得到上传的文件
      private String uploadImageContentType;//得到文件的类型
      private String uploadImageFileName;//得到文件的名称
      //这里略省了属性的getter/setter方法
      public String upload() throws Exception{
        String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        File file = new File(realpath);
        if(!file.exists()) file.mkdirs();
        FileUtils.copyFile(uploadImage, new File(file, uploadImageFileName));
        return "success";
      }
    }

    二:多文件上传

    第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:

    public class HelloWorldAction{
      private File[] uploadImages;//得到上传的文件
      private String[] uploadImagesContentType;//得到文件的类型
      private String[] uploadImagesFileName;//得到文件的名称
      //这里略省了属性的getter/setter方法
      public String upload() throws Exception{
        String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        File file = new File(realpath);
        if(!file.exists()) file.mkdirs();
        for(int i=0 ;i<uploadImages.length; i++){ File uploadImage = uploadImages[i];
        FileUtils.copyFile(uploadImage, new File(file, uploadImagesFileName[i]));
    }
        return "success";
      }}
  • 相关阅读:
    MySQL:解决脏读问题
    MySQL:隔离性问题(脏读)&回滚演示
    MySQL: Mysql 事务隔离级别
    MySQL:数据库事务
    GRE Vocabulary:sedulous
    MySQL:SQL约束
    GRE Vocabulary:pall
    MySQL:DQL操作单表
    MySQL: DQL 查询表中数据
    MySQL:DML操作 表中数据
  • 原文地址:https://www.cnblogs.com/feimo/p/2943654.html
Copyright © 2011-2022 走看看