管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-10-19 b1f4fd76dbc3eaafe9296fe35b28b4e65510e367
src/main/java/com/lf/server/service/data/UploaderService.java
@@ -6,6 +6,8 @@
import com.lf.server.entity.all.ResponseMsg;
import com.lf.server.entity.all.SettingData;
import com.lf.server.entity.ctrl.FileInfo;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.entity.sys.AttachEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.FileHelper;
@@ -31,7 +33,9 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.*;
/**
@@ -67,6 +71,7 @@
            String filePath = pathHelper.getTempPath() + File.separator + oldName;
            File newFile = new File(filePath);
            file.transferTo(newFile);
            long sizes = file.getSize();
            // 获取MD5
            String md5 = getFileMd5(filePath);
@@ -76,7 +81,7 @@
                return ctrl.success("文件已存在", md5);
            }
            AttachEntity ae = getAttachEntity(ue, tab, oldName, md5);
            AttachEntity ae = getAttachEntity(ue, tab, oldName, md5, sizes);
            String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath();
            newFile.renameTo(new File(targetPath));
@@ -88,6 +93,9 @@
        }
    }
    /**
     * 获取文件 MD5 码
     */
    private String getFileMd5(String filePath) throws IOException {
        FileInputStream fileStream = new FileInputStream(filePath);
        String md5 = DigestUtils.md5Hex(fileStream);
@@ -96,13 +104,17 @@
        return md5;
    }
    protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5) {
    /**
     * 获取附件实体类
     */
    protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5, long sizes) {
        AttachEntity entity = new AttachEntity();
        entity.setName(oldName);
        entity.setTab(tab);
        entity.setGuid(md5);
        String subPath = PathHelper.getUploadPath() + File.separator + md5;
        entity.setPath(subPath);
        entity.setSizes(sizes);
        if (ue != null) {
            entity.setCreateUser(ue.getId());
        }
@@ -113,7 +125,7 @@
    /**
     * 下载文件
     */
    public void download(String guid, HttpServletRequest req, HttpServletResponse res, BaseController ctrl) {
    public void download(String guid, HttpServletResponse res) {
        try {
            if (StringHelper.isEmpty(guid)) {
                WebHelper.write2Page(res, NO_FILE);
@@ -156,6 +168,9 @@
        }
    }
    /**
     * 设置下载响应信息
     */
    private void setDownloadResponse(AttachEntity entity, HttpServletResponse res) throws IOException {
        String fileName = URLEncoder.encode(entity.getName(), "UTF-8");
@@ -173,17 +188,61 @@
    /**
     * 上传文件
     */
    public Object uploadData(HttpServletRequest request, HttpServletResponse res) throws Exception {
    public Object uploadData(UserEntity ue, HttpServletRequest request, HttpServletResponse res) throws Exception {
        StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
        req.setCharacterEncoding("utf-8");
        res.setContentType("application/json;charset=utf-8");
        Map<String, String> map = getParams(req);
        List<FileInfo> list = getFiles(req);
        MetaEntity me = getMetaEntity(req);
        List<MetaFileEntity> list = getFiles(req);
        //
        return list.size();
    }
    private MetaEntity getMetaEntity(StandardMultipartHttpServletRequest req) {
        MetaEntity me = new MetaEntity();
        Enumeration<String> enumeration = req.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            try {
                Field field = me.getClass().getDeclaredField(key);
                if (field != null) {
                    field.setAccessible(true);
                    String value = req.getParameter(key);
                    switch (field.getType().toString()) {
                        case "class java.lang.String":
                            field.set(me, value);
                            break;
                        case "long":
                            field.set(me, Long.valueOf(value));
                            break;
                        case "int":
                            field.set(me, Integer.valueOf(value));
                            break;
                        case "class java.sql.Timestamp":
                            field.set(me, Timestamp.valueOf(value));
                            break;
                        default:
                            field.set(me, value);
                            break;
                    }
                }
            } catch (Exception ex) {
                //
            }
        }
        return me;
    }
    /**
     * 获取参数
     * Enumeration<String> headers = req.getHeaderNames();
     * Enumeration<String> attributes = req.getAttributeNames();
     */
    private Map<String, String> getParams(StandardMultipartHttpServletRequest req) {
        Map<String, String> map = new HashMap<String, String>(3);
@@ -197,24 +256,29 @@
        return map;
    }
    private List<FileInfo> getFiles(StandardMultipartHttpServletRequest req) throws Exception {
        List<FileInfo> list = new ArrayList<FileInfo>();
    /**
     * 获取文件
     */
    private List<MetaFileEntity> getFiles(StandardMultipartHttpServletRequest req) throws Exception {
        List<MetaFileEntity> list = new ArrayList<MetaFileEntity>();
        String path = pathHelper.getTempPath();
        Iterator<String> iterator = req.getFileNames();
        while (iterator.hasNext()) {
            MultipartFile file = req.getFile(iterator.next());
            FileInfo fi = new FileInfo(file.getOriginalFilename());
            if (StringHelper.isEmpty(fi.getFileName())) {
            if (StringHelper.isEmpty(file.getOriginalFilename())) {
                continue;
            }
            fi.setSize(file.getSize());
            fi.setPath(path + File.separator + fi.getFileName());
            file.transferTo(new File(fi.getPath()));
            MetaFileEntity mf = new MetaFileEntity();
            mf.setName(file.getOriginalFilename());
            mf.setSizes(file.getSize());
            mf.setPath(path + File.separator + mf.getName());
            list.add(fi);
            file.transferTo(new File(mf.getPath()));
            mf.setGuid(getFileMd5(mf.getPath()));
            list.add(mf);
        }
        return list;