【java】文件格式转换

【前言】

    敲黑板、咳~本文涉及两个内容:1、将mp3转为wma,2、自定义批量转指定文件夹下的指定文件

【正文】

    mp3转wma,这个用到了jave,偶是maven项目,这个jar在maven仓库是没有足迹滴,所以怎么办?

    自问自答第一问:maven引入本地jar包

          电脑安装了maven,在cmd窗口:

mvn install:install-file -Dfile=D:\ojdbc7.jar -DgroupId=com.tech4j.driver -DartifactId=oracle-connector-java -Dversion=12.1 -Dpackaging=jar  
    在这段命令中,-Dfile参数指你自定义JAR包文件所在的路径,并依次指定了自定义的GroupId、ArtifactId和Version信息。 

通过这种方式,可以简单快速地将第三方JAR包安装到本地仓库中供Maven项目依赖使用。例如:

<dependency>  
    <groupId>com.tech4j.driver</groupId>    
    <artifactId>oracle-connector-java</artifactId>    
    <version>12.1</version>    
</dependency> 

今天2018年6月9日09:53:22用powershell运行这个命令报错,win10:

The goal you specified requires a project to execute but there is no POM in this directory (F:\ITOOjar). Please verify you invoked Maven from the correct directory.

看信息挺明显的,找一个maven工程进入其根目录(有pom.xml的地方)运行上面的运行OK了;

上网说这样可能项目打包的时候打不了这个jar,所以保险起见,在pom文件中添加(关于这块,建议大家再上网确定一下)

   <!--引用本地jar-->
    <repositories>
        <repository>
            <id>jave1.0.2</id>
            <url>file://${project.basedir}/repo</url>//根路径下新建repo文件夹,把下载的jave**.jar放进去
        </repository>
    </repositories>

正式启用:

   /**
     * 利用jave 进行转换
     *
     * @param source
     * @param desFileName
     * @return
     * @throws EncoderException
     */
    public File execute(File source, String desFileName) throws EncoderException {
        File target = new File(desFileName);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        audio.setBitRate(new Integer(1280000));//不同格式,数值不同
        audio.setChannels(new Integer(2));
        audio.setSamplingRate(new Integer(44100));
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        encoder.encode(source, target, attrs);
        return target;
    }

    /**
     * 单个文件转换 ok 利用 jave.jar maven仓库没有
     * 需要引用本地的jar 固定线程池 批量 转
     * <dependency>
     * <groupId>com.giska.jave</groupId>
     * <artifactId>jave</artifactId>
     * <version>1.0.2</version>
     * </dependency>
     */
    @Test
    public void testJaveFiel() {
        File file = new File("E:\\image\\Sleep Away.mp3");//待转换的文件
        try {
            execute(file, "E:\\image\\Sleep Away.wav");//待转换的文件,换成成什么文件
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }

上面就ok了,进入下一环节,转换指定文件夹下指定文件(批量哦)

 //https://zhidao.baidu.com/question/617779174285688372.html

    /**
     * 转文件夹的文件格式 可指定 将**转为**
     * @throws IOException
     */
    @Test
    public void testTurnFile() throws IOException {
        /**
         *  String 读取的文件夹路径
         *  String 后缀名,|分隔
         *  String 新的后缀名
         */
        convertSuffix("E:\\image", "mp3", "wav");
    }

    public static final String SEPARATOR = System.getProperty("file.separator");
    //转换之后保存的路径
    private static final String SAVE = "E:\\image\\ne";

    /**
     * 递归读取文件夹中的特定后缀的文件
     * https://blog.csdn.net/zhpengfei0915/article/details/20614639
     *
     * @param path      String 读取的文件夹路径
     * @param suffix    String 后缀名,|分隔
     * @param newSuffix String 新的后缀名
     */
    public static void convertSuffix(String path, final String suffix, final String newSuffix) throws IOException {
        File p = new File(path);
        String name = p.getName(), regex = "(?i)([^\\.]*)\\.(" + suffix + ")";
        //如果path表示的是一个目录则返回true
        if (p.isDirectory()) {
            p.list(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    if (dir.isDirectory()) {
                        try {
                            convertSuffix(dir.getAbsolutePath() + SEPARATOR + name, suffix, newSuffix);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    return false;
                }
            });
        } else if (name.matches(regex)) {
            saveFiles(path, name, newSuffix);
        }
    }

    /**
     * 读取特定的后缀,修改后缀、保存文件
     *
     * @param path      读取文件夹的路径
     * @param name      特定后缀的文件名
     * @param newSuffix 新的后缀名
     */
    public static void saveFiles(String path, String name, String newSuffix) throws IOException {
        File fp = new File(path);
        if (!fp.exists()) {
            fp.mkdir();
        }
        name = name.replaceAll("([^\\.]+)(\\..*)?", "$1." + newSuffix);
        InputStream is = new FileInputStream(path);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        baos.flush();
        baos.close();
        is.close();
        byte[] data = baos.toByteArray();
        FileOutputStream fos = new FileOutputStream(new File(SAVE + SEPARATOR + name));
        fos.write(data);
        fos.flush();
        fos.close();
    }

小结:

    宏观看一遍,感觉差不多、先敲再说,网络知识一个好东西,几乎你想要的这里都有大笑


感谢分享:

https://blog.csdn.net/wabiaozia/article/details/52798194

https://zhidao.baidu.com/question/617779174285688372.html

还有些文章,忘记了连接、谢谢大家的分享


猜你喜欢

转载自blog.csdn.net/ma15732625261/article/details/80607690