上传文件,不显示大小

    @SneakyThrows
    @ApiOperation(value = "上传文件")
    @RecordLog(actionType = OPERATE_TYPE_2, descrption = "上传文件")
    @AccessLimit(needLogin = false)
    @RequestMapping("/api/Files")
    public ResultVo apiFiles (MultipartFile file)  {


            //获取原始文件名称
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀名
            String extension = "." + FilenameUtils.getExtension(originalFilename); //.jpg
            //获取新文件名称 命名:时间戳+UUID+后缀
            String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
                    + UUID.randomUUID().toString().substring(0, 4)
                    + extension;

            //获取资源路径 classpath的项目路径+/static/files  classpath就是resources的资源路径

        String path = "";
        if (JudgeSystem.JudgeSystem()){
            path = "/usr/local/src/wj/mp/";
        }else{
             path = ResourceUtils.getURL("classpath:").getPath() + "static/files/";
        }

            //新建一个时间文件夹标识,用来分类
//            String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            //全路径(存放资源的路径) 资源路径+时间文件夹标识
            String dataDir = path;
            System.out.println(dataDir);

            //全路径存放在文件类中,判断文件夹是否存在不存在就创建
            File dataFile = new File(dataDir);  //也可以直接放进去进行拼接 File dataFile = new File(path,format);
            if (!dataFile.exists()) {
                dataFile.mkdirs();
            }
            //文件上传至指定路径
            file.transferTo(new File(dataFile, newFileName));

//            //封装进实体类
//            fileUpload.setOldFileName(originalFilename);
//            fileUpload.setNewFileName(newFileName);
//            fileUpload.setExt(extension);
//            fileUpload.setPath("/files/" + format);
//            fileUpload.setGlobalPath(dataDir);
//            fileUpload.setDownCounts(0);
//            fileUpload.setType(type);
//            fileUpload.setSize(size);
//            fileUpload.setUploadTime(new Date());
//            User user = (User) session.getAttribute("user");
//            fileUpload.setUserId(user.getId());

        return ResultVo.success("https://lljt.agedlife.cn/mp/"+newFileName);

    }




package com.demo.utils;


import org.junit.Test;

/**
 * @description: 判断运行的系统是windows还是linux
 * @author: zhangyu
 */
public class JudgeSystem {
    public static boolean isLinux() {
        return System.getProperty("os.name").toLowerCase().contains("linux");
    }
 
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }
 
    public static Boolean JudgeSystem() {
        if (isLinux()) {
            return true;
        } else if (isWindows()) {
            return false;
        }
        return true;
    }
 
    @Test
    public void fun() {
        boolean flag1 = isLinux();
        // System.out.println(flag1);
        boolean flag2 = isWindows();
        // System.out.println(flag2);
        // System.out.println(System.getProperty("os.name"));
        System.out.println(JudgeSystem());
    }
}
使用java执行luinx命令



package org.ml.deployer.util;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
 
public class CommandUtil {
	public static String run(String command) throws IOException {
		Scanner input = null;
		String result = "";
		Process process = null;
		try {
			process = Runtime.getRuntime().exec(command);
			try {
				//等待命令执行完成
				process.waitFor(10, TimeUnit.SECONDS);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			InputStream is = process.getInputStream();
			input = new Scanner(is);
			while (input.hasNextLine()) {
				result += input.nextLine() + "\n";
			}
			result = command + "\n" + result; //加上命令本身,打印出来
		} finally {
			if (input != null) {
				input.close();
			}
			if (process != null) {
				process.destroy();
			}
		}
		return result;
	}
	
	public static String run(String[] command) throws IOException {
		Scanner input = null;
		String result = "";
		Process process = null;
		try {
			process = Runtime.getRuntime().exec(command);
			try {
				//等待命令执行完成
				process.waitFor(10, TimeUnit.SECONDS);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			InputStream is = process.getInputStream();
			input = new Scanner(is);
			while (input.hasNextLine()) {
				result += input.nextLine() + "\n";
			}
			result = command + "\n" + result; //加上命令本身,打印出来
		} finally {
			if (input != null) {
				input.close();
			}
			if (process != null) {
				process.destroy();
			}
		}
		return result;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值