使用URI复制时不支持文件名中的特殊字符 [英] Special character in filename are not supported while copying using URI

查看:1474
本文介绍了使用URI复制时不支持文件名中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用URI将文件(文件名包含特殊字符)从一个路径复制到另一个路径。但它抛出一个错误。如果成功复制,如果文件名不包含特殊字符。你能告诉我如何使用URI从一个路径到另一个路径复制具有特殊字符的文件名。我已复制下面的代码和错误。

I need to copy the files(file name contains special character) from one path to another path using URI. But its throws an error. If its successfully copied, if the filename not contains special character. Could you please advise me how to copy the file name with special character using URI from one path to another path. I have copied the code and error below.

代码:

import java.io.*;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class test {
    private static File file = null;
    public static void main(String[] args) throws InterruptedException, Exception {
        String from = "file:///home/guest/input/3.-^%&.txt";
        String to = "file:///home/guest/output/3.-^%&.txt";
        InputStream in = null;
        OutputStream out = null;
        final ReadableByteChannel inputChannel;
        final WritableByteChannel outputChannel;
        if (from.startsWith("file://")) {
            file = new File(new URI(from));
            in = new FileInputStream(file);
        }

        if (from.startsWith("file://")) {
            file = new File(new URI(to));
            out = new FileOutputStream(file);
        }

        inputChannel = Channels.newChannel(in);
        outputChannel = Channels.newChannel(out);

        test.copy(inputChannel, outputChannel);
        inputChannel.close();
        outputChannel.close();
    }

    public static void copy(ReadableByteChannel in, WritableByteChannel out) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);
        while (in.read(buffer) != -1 || buffer.position() > 0) {
        buffer.flip();
        out.write(buffer);
        buffer.compact();
        }
  }
}

-

Exception in thread "main" java.net.URISyntaxException: Illegal character in path at index 30: file:///home/maria/input/3.-^%&.txt
    at java.net.URI$Parser.fail(URI.java:2829)
    at java.net.URI$Parser.checkChars(URI.java:3002)
    at java.net.URI$Parser.parseHierarchical(URI.java:3086)
    at java.net.URI$Parser.parse(URI.java:3034)
    at java.net.URI.<init>(URI.java:595)
    at com.tnq.fms.test3.main(test3.java:29)
Java Result: 1

感谢您查看...

推荐答案

文件名应为%-escaped 。例如,实际文件名中的空格在URI中变为%20。如果您使用带有多个参数的构造函数, java.net.URI 类可以为您完成:

The filenames should be %-escaped. For example, a space in the actual filename becomes a %20 in the URI. The java.net.URI class can do it for you if you use one of the constructors with several arguments:

new URI("file", null, "/home/guest/input/3.-^%&.txt", null);

请参阅 HTTP URL地址在Java中的编码

这篇关于使用URI复制时不支持文件名中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆