UploadUtil导入工具类

本文档介绍如何利用UploadUtil工具类进行文件上传操作,包括导入步骤和使用示例,帮助开发者高效处理文件上传任务。
摘要由CSDN通过智能技术生成
package com.common.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class UploadUtil {

    private static String uploadPath;

    @Value("${upload.path}")
    public void setUploadPath(String uploadPath) {
        UploadUtil.uploadPath = uploadPath;
    }

    /**
     * 上传文件
     *
     * @param applicationName
     * @param file
     * @return
     */
    public static Map upload(String applicationName, MultipartFile file) {
        Map map = new HashMap();
        String fileName = file.getOriginalFilename();
        map.put("name", fileName);

        String type = fileName.substring(fileName.lastIndexOf("."));
        fileName = new Date().getTime() + type;
        File dest = new File(uploadPath + applicationName + "/" + fileName);
        if (!dest.getParentFile().exists()) { // 判断文件父目录是否存?
            dest.getParentFile().mkdirs();
        }
        map.put("type", type);
        map.put("filePath", applicationName + "/" + fileName);
        map.put("localFilePath", uploadPath + applicationName + "/" + fileName);

        try {
            file.transferTo(dest); // 保存文件
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return map;
    }


    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路? 如:c:/fqf.txt
     * @param newPath String 复制后路? 如:f:/fqf.txt
     * @return boolean
     */
    @SuppressWarnin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值