Java基于Windows下的文件系统

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_31122833/article/details/89945980

1、其实非常简单,就是使用流

public class FileApi {
    public static final String FILE_SYSTEM="C:\\FileSystem\\";
    /**
     * 上传文件
     * @param file 文件
     * @param fileName 给新文件名
     * @return
     */
    public static Result upload(MultipartFile file, String fileName){
        Result result = new Result();
        if(file!=null){
            if(file.getSize()<10*1024*1024){//不能超过10MB
                StringBuffer file_path = new StringBuffer(FILE_SYSTEM);
                file_path.append(fileName==null?file.getOriginalFilename():fileName+getAfterFileName(file.getOriginalFilename()));
                File to_file = new File(file_path.toString());//拷贝过去的文件
                try {
                    //准备流
                    InputStream fin = file.getInputStream();//文件输入流:读文件
                    FileOutputStream fout = new FileOutputStream(to_file);//文件输出流:写文件
                    //定义数组:用于缓冲,可以加快文件复制的速度
                    byte[] arr = new byte[1024];
                    //读文件
                    int length = fin.read(arr);//length,表示读出来的长度,返回读取的字节个数
                    while(length != -1){//当读到负一  表示结束
                        //写文件
                        fout.write(arr,0,length);//重点,要去掉不必要的
                        length = fin.read(arr);//继续读取
                    }
                    fin.close();//关闭流
                    fout.flush();//对于输出流,先刷新
                    fout.close();//再关闭
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                result.setType(TypeEnum.SUCCESS.getCode());
                result.setMessage("上传成功");
                result.setData(file_path);
            }else{
                result.setType(TypeEnum.FAIL.getCode());
                result.setMessage("文件太大:"+file.getSize()/(1024*1024)+"MB");
            }
        }else{
            result.setType(TypeEnum.FAIL.getCode());
            result.setMessage("请上传文件");
        }
        return result;
    }
    /**
     * 根据地址删除文件
     * @param filePath
     * @return
     */
    public static Result deleteFile(String filePath){
        Result result = new Result();
        if(CheckUtil.checkNull(filePath)){
            result.setType(TypeEnum.FAIL.getCode());
            result.setMessage("请输入地址");
        }else{
            File file = new File(filePath);
            if(!file.exists()){
                result.setType(TypeEnum.FAIL.getCode());
                result.setMessage("文件不存在");
            }else{
                result.setType(TypeEnum.SUCCESS.getCode());
                result.setMessage("删除成功");
                file.delete();
            }
        }
        return result;
    }
    /**
     * 根据地址返回流
     * @param filePath
     * @return
     * @throws Exception
     */
    public static InputStream getInputStream(String filePath) throws Exception{
        if(!CheckUtil.checkNull(filePath)) return new FileInputStream(new File(filePath));
        return null;
    }
    /**
     * 获取文件前缀名 test
     * @param fileName
     * @return
     */
    public static String getPreFileName(String fileName){
        return fileName.substring(0,fileName.lastIndexOf("."));
    }
    /**
     * 获取文件前缀名 .txt
     * @param fileName
     * @return
     */
    public static String getAfterFileName(String fileName){
        return fileName.substring(fileName.lastIndexOf("."),fileName.length());
    }

}

2、使用基于Windows下的文件系统有一个好处就是,开发人员可以在本地进行研发测试;但部署服务器需要使用Windwos系统的

猜你喜欢

转载自blog.csdn.net/qq_31122833/article/details/89945980