博客
关于我
java文件上传带进度条_java文件上传带进度条的
阅读量:791 次
发布时间: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/

你可能感兴趣的文章
DRBD分布式存储解决方案实战
查看>>
DRBL+Clonezilla全自动批量安装操作系统
查看>>
DSMM数据安全概述
查看>>
Dva员工增删改查Demo实现-优化
查看>>
EasyUi的使用与代码编写(一)
查看>>
eclipse配置tomcat8.5报错The Apache Tomcat installation at this directory is version 8.5.4. A Tomcat
查看>>
eclipse配置xml的自动提示
查看>>
"不能为虚拟电脑 ubuntu 打开一个新任务"的解决办法
查看>>
eclipse重置页面恢复到最初布局状态
查看>>
ecmall开发记录(一)
查看>>
ecplise中创建jsp页面时默认的编码格式为ISO-8859-1,这里我们将其编码格式设置为utf-8...
查看>>
ECSHOP实现收货国家省市由选择下拉菜单改为手动
查看>>
ECShop模板原理
查看>>
edgeboxes proposal 和dpm 连接
查看>>
EdgeX Foundry:开启边缘计算新时代
查看>>
EdgeX Foundry:边缘计算的创新平台
查看>>
EdgeX Foundry:边缘计算的未来趋势与应用
查看>>
Edge浏览器打开控制台后程序总是停止进入debug模式关闭教程【八仙过海之又一过海方案】
查看>>
Educational Codeforces Round 28
查看>>
Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并
查看>>