博客
关于我
java文件上传带进度条_java文件上传带进度条的
阅读量:804 次
发布时间:2023-01-24

本文共 2056 字,大约阅读时间需要 6 分钟。

file upload controller class for handle multipart file upload requestcom.controller.FileUploadControllerclass FileUploadController implements file upload functionality with progress trackinglog = logger.getLogger(FileUploadController.class)upload method implementation@Controller@RequestMapping("/upload.html")public ModelAndView upload(HttpServletRequest request, HttpServletResponse response) throws Exception {create session instancesession = request.getSession()modelAndView = new ModelAndView()check if request is multipartisMultipart = ServletFileUpload.isMultipartContent(request)if (!isMultipart) {    return modelAndView;}create disk file item factoryFileItemFactory factory = new DiskFileItemFactory();initialize file upload serviceuploadService = new ServletFileUpload(factory);set progress listeneruploadService.setProgressListener(new ProgressListener() {    // progress updating logic    public void update(long bytesRead, long contentLength, int items) {        ProcessInfo info = new ProcessInfo();        info.itemNum = items;        info.readSize = bytesRead;        info.totalSize = contentLength;        info.show = String.format("%d/%d byte", bytesRead, contentLength);        info.rate = (int)(Math.round((float)bytesRead / contentLength * 100));        session.setAttribute("processInfo", info);    }});parse requestitems = uploadService.parseRequest(request)process uploaded itemsforeach (item in items) {    if (item is form field) {        // handle common form fields    } else {        // handle file upload fields        String fileName = item.getName();        String contentType = item.getContentType();        File uploadedFile = new File(("c://" + fileName));        item.write(uploadedFile);    }}return modelAndView}process method for getting progress info@RequestMapping("/process.json", method=RequestMethod.GET)@ResponseBodypublic Object process(HttpServletRequest request, HttpServletResponse response) throws Exception {    return session.getAttribute("processInfo");}class ProcessInfo {    // properties and fields}

转载地址:http://dmeyk.baihongyu.com/

你可能感兴趣的文章
mysql 判断表字段是否存在,然后修改
查看>>
MySQL 到底能不能放到 Docker 里跑?
查看>>
mysql 前缀索引 命令_11 | Mysql怎么给字符串字段加索引?
查看>>
MySQL 加锁处理分析
查看>>
mysql 协议的退出命令包及解析
查看>>
mysql 参数 innodb_flush_log_at_trx_commit
查看>>
mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
查看>>
MySQL 命令和内置函数
查看>>
MySQL 和 PostgreSQL,我到底选择哪个?
查看>>
mysql 四种存储引擎
查看>>
MySQL 在并发场景下的问题及解决思路
查看>>
MySQL 在控制台插入数据时,中文乱码问题的解决
查看>>
MySQL 基础架构
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 处理插入重主键唯一键重复值办法
查看>>
MySQL 备份 Xtrabackup
查看>>
mysql 复杂查询_mysql中复杂查询
查看>>
mYSQL 外键约束
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>