SSM框架如何实现ftp上传文件进度回调

SSM框架如何实现ftp上传文件进度回调

  1. 创建监听器,监听器需实现ProgressListener类

import controller.ViewObject.ProgressEntity;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

@Component
public class MyProgressListener implements ProgressListener {

    private HttpSession session;

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
        ProgressEntity status = new ProgressEntity();
        session.setAttribute("status", status);
    }


    /*
     * pBytesRead 到目前为止读取文件的比特数 pContentLength 文件总大小 pItems 目前正在读取第几个文件
     */
    public void update(long pBytesRead, long pContentLength, int pItems) {
        ProgressEntity status = (ProgressEntity) session.getAttribute("status");
        status.setpBytesRead(pBytesRead);
        status.setpContentLength(pContentLength);
        status.setpItems(pItems);
    }
}
  1. 创建解析器,解析器需继承CommonsMultipartResolver类

import controller.listener.MyProgressListener;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CustomMultipartResolver extends CommonsMultipartResolver{

    @Autowired
    private MyProgressListener progressListener;

    public void setProgressListener(MyProgressListener progressListener){
        this.progressListener=progressListener;
    }

    @Override
    /*@SuppressWarnings("unchecked")*/
    public MultipartParsingResult parseRequest(HttpServletRequest request)
            throws MultipartException {
        String encoding = determineEncoding(request);
        FileUpload fileUpload = prepareFileUpload(encoding);
        progressListener.setSession(request.getSession());
        fileUpload.setProgressListener(progressListener);
        try {
            List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
            return parseFileItems(fileItems, encoding);
        } catch (FileUploadBase.SizeLimitExceededException ex) {
            throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
        } catch (FileUploadException ex) {
            throw new MultipartException("Could not parse multipart servlet request", ex);
        }
    }

    /*https://blog.csdn.net/Dopamy_BusyMonkey/article/details/51823414*/
}
  1. 创建实体类
public class ProgressEntity {

    private long pBytesRead = 0L;   //到目前为止读取文件的比特数
    private long pContentLength = 0L;    //文件总大小
    private int pItems;                //目前正在读取第几个文件

    public long getpBytesRead() {
        return pBytesRead;
    }
    public void setpBytesRead(long pBytesRead) {
        this.pBytesRead = pBytesRead;
    }
    public long getpContentLength() {
        return pContentLength;
    }
    public void setpContentLength(long pContentLength) {
        this.pContentLength = pContentLength;
    }
    public int getpItems() {
        return pItems;
    }
    public void setpItems(int pItems) {
        this.pItems = pItems;
    }
    @Override
    public String toString() {
        float tmp = (float)pBytesRead;
        float result = tmp/pContentLength*100;
        return "ProgressEntity [pBytesRead=" + pBytesRead + ", pContentLength="
                + pContentLength + ", percentage=" + result + "% , pItems=" + pItems + "]";
    }
}

  1. 在xml里面配置解析器
<bean id="multipartResolver" class="controller.resolver.CustomMultipartResolver">
        <property name="maxUploadSize" value="10240000000"/>
        <property name="maxInMemorySize" value="4096"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
  1. 创建controller类
    @RequestMapping("/uploadToFtpServer")
    @ResponseBody
    public ResponseUtil upload(VideoFtpVo videoFtpVo, @RequestParam CommonsMultipartFile file){
        ResponseUtil responseUtil=new ResponseUtil();
        try {
            String name=RenameUtil.renameFilename(file.getOriginalFilename());
            String message=FtpUtil.uploadFile(name,file.getInputStream());
            VideoInfoVo videoInfoVo=new VideoInfoVo();
            BeanUtils.copyProperties(videoInfoVo,videoFtpVo);//注意是将后面的值赋给前面
            videoInfoVo.setFileName(message);
            /*String video_id=UploadVideo.UploadVideo(videoInfoVo);*/
            Integer user_id=Integer.parseInt(RedisUtil.get("user_id"));
            Integer video_id=videoService.addVideoToFtp(videoInfoVo);
            /*videoService.addVideoId(user_id,video_id,VedioFtpVo.getTitle());*/
            responseUtil.setStatus(200);
            responseUtil.setMessage(video_id.toString());
            return responseUtil;
        } catch (IOException e) {
            e.printStackTrace();
            responseUtil.setStatus(500);
            responseUtil.setMessage("上传失败!"+e);
            return responseUtil;
        }
    }
    @RequestMapping("/callback")
    @ResponseBody
    public String callback(HttpServletRequest httpServletRequest){
        ProgressEntity status= (ProgressEntity) httpServletRequest.getSession().getAttribute("status");
        try {
            if (status!=null){
                long byteRead=status.getpBytesRead();
                long contentLength=status.getpContentLength();
                NumberFormat numberFormat=NumberFormat.getPercentInstance();//这句是什么意思
                numberFormat.setMinimumFractionDigits(2);
                numberFormat.setRoundingMode(RoundingMode.HALF_UP);
                String percent = numberFormat.format(byteRead*1.0/contentLength);//上传文件的百分比
                return percent;
            }else
                return "0.00";
        } catch (Exception e) {
            e.printStackTrace();
            return String.valueOf(e);
        }
    }

借鉴了https://blog.csdn.net/Dopamy_BusyMonkey/article/details/51823414的博客,稍作改动,不知道有没有侵权。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
一、简介 通过这个课程带大家从零开发一款功能全面的后台管理系统,包括项目搭建、功能实现到最后的Linux系统部署全过程。本课程使用SpringMVC + Spring + Mybatis作为主体框架,使用AdminLTE作为前端框架,使用主流关系型数据库Mysql作为存储数据库,使用非关系型数据库Redis作为缓存数据库,并集成SpringSecuriy安全框架做权限的动态管理,集成Swagger2自动生成接口文档,集成Druid连接池进行SQL性能监控,集成ActiveMQ消息中间件进行异步解耦,提高性能。最后使用linux系统进行服务部署,并搭建nginx反向代理服务器提高网站性能。 二、学习目标 通过本课程的学习带大家掌握SSM框架的开发流程,并熟练使用SpringSecurity做为安全框架进行权限管理,整合相关优秀的开源框架进行功能开发。还在项目中带大家学习前端相关的Jquery、Bootstrap等知识。课程结束之后希望大家能做到独立进行开发项目的目的,增强解决问题的能力,具备功能落地实现的能力。 三、课程涉及知识点 SpringMVC源码分析Mybatis源码分析通用MapperMysql数据库Redis缓存实现ActiveMQ消息中间件SpringSecurity鉴权Swagger2接口文档生成自定义注解AOP切面编程自定义过滤器Logback日志整合Druid性能监控Linux系统Nginx反向代理Ajax异步请求技术Jquery基本使用AdminLTE前端框架Chart图表-线状图和饼状图百度地图定位城市BootStrap前端框架BootStrap-Table插件BootStrap-Treeview插件Markdown编辑器403、404、500错误页面配置数据库事务消息提示插件toastr.js图片上传插件bootstrap fileinput数字滚动效果pv/uv流量统计...四、课程部分内容截图如下 1、首页 2、菜单管理 3、图床管理 4、图标管理 5、留言反馈管理 6、druid监控 7、登录日志
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值