文件上传file

废话不多说  直接上代码  有需要的直接拿去用 


package com.cn.auth.common;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * describe: 文件上传工具类
 * current user Mir wang
 * current system 2019/11/8
 */
public class FileUtil {

    /**
     * 上传图片返回图片位置信息
     * @param pathName 文件保存位置文件名
     * @param file 上传的文件信息
     * @param request  请求信息
     * @param response 请求信息
     * @return   图片位置信息
     * @throws IllegalStateException
     * @throws IOException
     */
    public static String uploadImage(String pathName, MultipartFile file,
                                     HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {
        String realPath = request.getSession().getServletContext().getRealPath("/");
        //重新定义文件保存路径
        realPath = getRenamePath(realPath, request);
        // 文件保存路径
        String fileDir = "/fileData/"+pathName+"/" ;
        //文件重命名
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        suffix = suffix.trim();
        String newFileName = fileName;
        if(null!=suffix){
            newFileName = DateUtil.getStringAllDate()+"."+suffix;
        }
        //查看文件是否存在
        File destFile = new File(realPath + fileDir);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        //创建文件
        File f = new File(destFile.getAbsoluteFile() + "\\" + newFileName);
        System.out.println("##############文件存储位置:"+destFile.getAbsoluteFile() + "\\" + newFileName);
        file.transferTo(f);
        f.createNewFile();
        String filePath = fileDir + newFileName;
        return filePath;
    }

    /**
     * 重新定义文件保存路径
     * @param realPath
     * @param request
     * @return
     */
    public static String getRenamePath(String realPath,HttpServletRequest request){
        //获取项目名称
        String projectName= request.getServletContext().getContextPath();
        projectName = projectName.replace("/", "");
        System.err.println("projectName="+projectName);
        System.out.println("request projectName="+request.getContextPath());
        if(StringUtil.isBlank(projectName))
        {
            projectName = "springboot-auth-up";
            realPath = realPath.replace(projectName, "");
        }else
        {
            realPath = realPath.replace(projectName, "");
        }
        return realPath;
    }


    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath+fileName);
        out.write(file);
        out.flush();
        out.close();
    }
    /**
     * 上传文件返回图片位置信息
     * @param pathName 文件保存位置文件名
     * @param file 上传的文件信息
     * @param request  请求信息
     * @param response 请求信息
     * @return   图片位置信息
     * @throws IllegalStateException
     * @throws IOException
     */
    public static BufferedReader getBufferedReader(HttpServletRequest request, MultipartFile multipartFile, String fileName) throws IOException {
        String realPath = request.getSession().getServletContext().getRealPath("/");
        //重新定义文件保存路径
        realPath = getRenamePath(realPath, request);
        // 文件保存路径
//                String fileDir = "/fileData/"+pathName+"/" ;
        String fileDir = "/fileData/orderCsv/" ;
        //文件重命名
//                String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        suffix = suffix.trim();
        String newFileName = fileName;
        if(null!=suffix){
            newFileName = DateUtil.getStringAllDate()+"."+suffix;
        }
        //查看文件是否存在
        File destFile = new File(realPath + fileDir);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        //创建文件
        File f = new File(destFile.getAbsoluteFile() + "\\" + newFileName);
        System.out.println("##############文件存储位置:"+destFile.getAbsoluteFile() + "\\" + newFileName);
        multipartFile.transferTo(f);
        f.createNewFile();
//                String path = fileDir + newFileName;
        BufferedReader reader = null;
        reader=new BufferedReader((new InputStreamReader(new FileInputStream(f),"UTF-8")));
        return reader;
    }
    public static void main(String[] args) {

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值