博客
关于我
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/

你可能感兴趣的文章
多线程,高并发
查看>>
linux(CENTOS)系统各个目录的作用详解
查看>>
回溯法介绍
查看>>
2025最新智能优化算法:改进型雪雁算法(Improved Snow Geese Algorithm, ISGA)求解23个经典函数测试集
查看>>
有了Trae,人人都是程序员的时代来了
查看>>
Servlet的三个基本方法
查看>>
基于Trae AI的新SSH Remote功能:服务器Python接口日志排查实战与技术展望
查看>>
反 TypeScript
查看>>
数据分析与处理方法
查看>>
打开有惊喜
查看>>
AUTOSAR_SWS_CANDriver4
查看>>
程序员都看不懂的代码
查看>>
LLM+多智能体协作:基于CrewAI与DeepSeek的邮件自动化实践
查看>>
404页面自动跳转源码
查看>>
458. 可怜的小猪
查看>>
46:把数字翻译成字符串(动态规划)
查看>>
49天精通Java,第28天,Java lambda表达式
查看>>
500套精美Logo样机模板可直接套用、轻松制作炫酷logo
查看>>
5小时内使用DeepSeek写出一篇优质论文的三步攻略指南
查看>>
60天新媒体公众号写作秘诀
查看>>