zoukankan      html  css  js  c++  java
  • commons-fileupload 组件实现文件上传

      index.jsp

    核心代码:

        //创建文件项工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //创建解析请求 数据的ServletFileUpload对象
            ServletFileUpload upload = new ServletFileUpload();
            //解析请求数据 返回FileItem 列表
            List<FileItem> list = upload.parseRequest(request);
            //获取每一个FileItem 对象
            FileItem item = list.get(i);
            //验证当前FileItem  是否是表单字段    如果fales  则取到的是文件
            item.isFormField();
            
            //处理文件
            String filename= item.getName();
            //截取文件扩展名
            String extName = filename.substring(filename.lastIndexOf("."));
            //生成UUID作为文件名
            String newName= UUID.randomUUID().toString();
            //获取服务器上自定义的存放文件的目录
            String rootPath = request.getSession().getServletContext().getRealPath("/upload");
            //生成完整的文件路径
            String newPath = rootPath+"/"+newName+extName; 
            //写入文件
            item.write(new File(newPath));
  • 相关阅读:
    函数模板——隐式实例化、显式实例化、显式具体化
    SQLAlchemy
    pymysql的使用
    mysql 安装
    Django---Cerley使用
    支付宝支付功能
    Django--log配置
    Vue--基础
    Python学习手册
    针对特定网站scrapy爬虫的性能优化
  • 原文地址:https://www.cnblogs.com/the-wang/p/7545754.html
Copyright © 2011-2022 走看看