Java-本地文件上传、重命名、移动、删除

package com.common.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Description 本地文件上传
 * @Author FangYN
 * @Date 2019/12/25
 **/
public class FileUtils {
    public static final String imagePath = "/upload/image/";

    /**
     * 上传文件至本地
     * 存储原图地址如:http://127.0.0.1:8080:/upload/image/20170804/1501811601145.jpg
     * @param muFile
     * @param pathFileName 文件夹名
     * @param fileName 文件名
     * @return  返回 图片保存路径  具体保存地址为 C:/upload/image/  下
     */
    public static String fileUpload(HttpServletRequest request, MultipartFile muFile, String pathFileName, String fileName) {
        //获取上传文件的保存路径:文件拼接地址:服务器本地保存路径+文件名+后缀
        String fileNamess = null;
        String getFileName = null;
        try {
            //文件路径如:C:/upload/image/文件名/
            String picPath = getUploadFilePath(request) + imagePath + pathFileName + "/";
            //根据真实路径创建目录文件
            File picSaveFile = new File(picPath);
            if(!picSaveFile.exists())
                picSaveFile.mkdirs();
            fileNamess = picPath + fileName ;
            File file = new File(fileNamess);
            muFile.transferTo(file);// 转存文件到指定的路径
            getFileName = imagePath + pathFileName + "/" + fileName;
            System.out.println("图片本地保存地址:" + (fileNamess));
            System.out.println("图片地址:" + (getFileName));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("创建本地文件失败"+e.getMessage());
        }
        return getFileName;
    }

    /**
     * 文件删除
     * @param fileNameUrl 需要删除的文件名称(完整的访问路径)
     * @return 是否删除成功
     */
    public static void fileDelete(HttpServletRequest request, String fileNameUrl){
        if(fileNameUrl == null || fileNameUrl.equals(""))
            return;
        String url = fileNameUrl.substring(0, fileNameUrl.lastIndexOf(imagePath));
        fileNameUrl = fileNameUrl.replace(url, getUploadFilePath(request));
        try{
            File f = new File(fileNameUrl);
            f.delete();
        }catch(Exception ex){
            ex.printStackTrace();
            System.out.println("文件删除出现错误!");
        }
    }

    /**
     * 文件重命名/移动文件
     * @param oldFileNameUrl
     * @param newFileName
     * @param filePath 文件夹
     * @return 图片保存路径(相对路径)
     */
    public static String fileRename(HttpServletRequest request, String oldFileNameUrl, String newFileName, String filePath){
        if(StringUtils.isBlank(newFileName) || StringUtils.isBlank(oldFileNameUrl))
            return "";
        String diskPath = getUploadFilePath(request);
        String path = diskPath + imagePath + filePath + "/";
        //根据真实路径创建目录文件
        File picSaveFile = new File(path);
        if(!picSaveFile.exists())
            picSaveFile.mkdirs();
        String url = oldFileNameUrl.substring(0, oldFileNameUrl.lastIndexOf(imagePath));
        oldFileNameUrl = StringUtils.isBlank(url) ? diskPath+oldFileNameUrl : oldFileNameUrl.replace(url, diskPath);
        try{
            File file=new File(oldFileNameUrl);
            if(file.exists()) {
                Integer suffixLength = oldFileNameUrl.lastIndexOf(".");
                String suffix = "";
                if (suffixLength != -1) {
                    suffix = oldFileNameUrl.substring(oldFileNameUrl.lastIndexOf("."));
                }
                String newFileNameUrl = path + newFileName + suffix;
                file.renameTo(new File(newFileNameUrl));
                return imagePath + filePath + "/" + newFileName + suffix;
            }
        }catch(Exception ex){
            ex.printStackTrace();
            System.out.println("文件重命名出现错误!");
        }
        return "";
    }

    public static Boolean isFileExist(HttpServletRequest request, String fileName, String filePath) {
        if (StringUtils.isBlank(fileName))
            return false;
        String diskPath = getUploadFilePath(request);
        String path = diskPath + imagePath + filePath + "/";
        //根据真实路径创建目录文件
        File picSaveFile = new File(path);
        if (!picSaveFile.exists())
            picSaveFile.mkdirs();
        try {
            String fileNameUrl = path + fileName;
            File file = new File(fileNameUrl);
            if (file.exists()) {
                return true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 获取当前项目所在根目录地址
     * @param request
     * @return
     */
    private static String getUploadFilePath(HttpServletRequest request){
        String savePath = request.getSession().getServletContext().getRealPath("");
        savePath = savePath.substring(0,savePath.lastIndexOf("\\"));
        return savePath+"/";
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值