java.io.FileNotFoundException: Invalid file path
	at java.io.FileOutputStream.<init>(FileOutputStream.java:206)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
	at com.crazymakercircle.iodemo.socketDemos.NioReceiveServer.processData(NioReceiveServer.java:148)
	at com.crazymakercircle.iodemo.socketDemos.NioReceiveServer.startServer(NioReceiveServer.java:101)
	at com.crazymakercircle.iodemo.socketDemos.NioReceiveServer.main(NioReceiveServer.java:194)
     * 处理客户端传输过来的数据
    private void processData(SelectionKey key) throws IOException {
        Client client = clientMap.get(key.channel());
        SocketChannel socketChannel = (SocketChannel) key.channel();
        int num = 0;
        try {
            buffer.clear();
            while ((num = socketChannel.read(buffer)) > 0) {
                buffer.flip();
                //客户端发送过来的,首先是文件名
                if (null == client.fileName) {
                    // 文件名
                    String fileName = charset.decode(buffer).toString();
                    String destPath = IOUtil.getResourcePath(NioDemoConfig.SOCKET_RECEIVE_PATH);
                    File directory = new File(destPath);
                    if (!directory.exists()) {
                        directory.mkdir();
                    client.fileName = fileName;
                    String fullName = directory.getAbsolutePath()
                            + File.separatorChar + fileName;
                    Logger.debug("NIO  传输目标文件:" + fullName);
                    File file = new File(fullName);
                    FileChannel fileChannel = new FileOutputStream(file).getChannel();
                    client.outChannel = fileChannel;
                //客户端发送过来的,其次是文件长度
                else if (0 == client.fileLength) {
                    // 文件长度
                    long fileLength = buffer.getLong();
                    client.fileLength = fileLength;
                    client.startTime = System.currentTimeMillis();
                    Logger.debug("NIO  传输开始:");
                //客户端发送过来的,最后是文件内容
                else {
                    // 写入文件
                    client.outChannel.write(buffer);
                buffer.clear();
            key.cancel();
        } catch (IOException e) {
            key.cancel();
            e.printStackTrace();
            return;
        // 调用close为-1 到达末尾
        if (num == -1) {
            IOUtil.closeQuietly(client.outChannel);
            System.out.println("上传完毕");
            key.cancel();
            Logger.debug("文件接收成功,File Name:" + client.fileName);
            Logger.debug(" Size:" + IOUtil.getFormatFileSize(client.fileLength));
            long endTime = System.currentTimeMillis();
            Logger.debug("NIO IO 传输毫秒数:" + (endTime - client.startTime));
 File file = new File(fullName);

创建了一个File对象,但是fullName代表的文件必须存在。但是根据代码业务逻辑。复制文件时,新文件一般是不存在的,所以必须手动新建文件。

                    File file = new File(fullName);
//                    文件不存在则创建文件
                    if (!file.exists()){
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();

这段代码的意思是,创建一个名为file的File对象,如果该文件不存在,则手动创建,再次运行,则此异常消失。

* 处理客户端传输过来的数据 private void processData(SelectionKey key) throws IOException { Client client = clientMap.get(key.channel()); SocketChannel socketChannel = (SocketChannel) key.channel(); int num = 0; try { buffer.clear(); while ((num = socketChannel.read(buffer)) > 0) { buffer.flip(); //客户端发送过来的,首先是文件名 if (null == client.fileName) { // 文件名 String fileName = charset.decode(buffer).toString(); String destPath = IOUtil.getResourcePath(NioDemoConfig.SOCKET_RECEIVE_PATH); File directory = new File(destPath); if (!directory.exists()) { directory.mkdir(); client.fileName = fileName; String fullName = directory.getAbsolutePath() + File.separatorChar + fileName; Logger.debug("NIO 传输目标文件:" + fullName); File file = new File(fullName); // 文件不存在则创建文件 if (!file.exists()){ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); FileChannel fileChannel = new FileOutputStream(file).getChannel(); client.outChannel = fileChannel; //客户端发送过来的,其次是文件长度 else if (0 == client.fileLength) { // 文件长度 long fileLength = buffer.getLong(); client.fileLength = fileLength; client.startTime = System.currentTimeMillis(); Logger.debug("NIO 传输开始:"); //客户端发送过来的,最后是文件内容 else { // 写入文件 client.outChannel.write(buffer); buffer.clear(); key.cancel(); } catch (IOException e) { key.cancel(); e.printStackTrace(); return; // 调用close为-1 到达末尾 if (num == -1) { IOUtil.closeQuietly(client.outChannel); System.out.println("上传完毕"); key.cancel(); Logger.debug("文件接收成功,File Name:" + client.fileName); Logger.debug(" Size:" + IOUtil.getFormatFileSize(client.fileLength)); long endTime = System.currentTimeMillis(); Logger.debug("NIO IO 传输毫秒数:" + (endTime - client.startTime));

写的不到位的地方还请多多指教!虚心向各位大神学习

文章目录异常信息2异常代码原因分析解决方案完整代码总结异常信息java.io.FileNotFoundException: Invalid file path at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:206) at java.io.FileOutputStream.&lt;init&gt;(FileOut...
01-错误信息: Exception in thread "main" java.io.FileNotFoundException: e:b (拒绝访问。) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOu
java.io.FileNotFoundException: class path resource [db/aixing.db] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Users/office/Desktop/card_active-0.0.1-SNAPSHOT.jar!/ BOOT-INF/classes!
java.io.FileNotFoundException:解决方法 运行如下代码会出现 java.io.FileNotFoundException:D:\IDEA%20Workspace\out\production\day04_JDBC\jdbc.properties (系统找不到指定的路径。) ```java public class test { public static void main(String[] args) { try { xnio创建channel的代码: 后出现报错信息:Exception in thread “main” java.io.IOException: Invalid file path。 在 jvm 参数中,添加-Djdk.io.File.enableADS=true即可。
一、问题 在使用FileInputStream或FileOutputStream时会遇到如下问题1和问题2。 问题1: java.io.FileNotFoundException: .\xxx\xxx.txt (系统找不到指定的路径。) at java.io.FileOu...
./gradlew run --args= " /path/to/file " 如果文件不存在给定的路径,或者您尝试在没有路径的情况下运行该应用程序,则该应用程序将显示一条错误消息。 例如: Invalid input file: /path/to/file. Input file cannot be blank. 如果提供有效的得分表文件,您将在控制台中看到结果: Frame 1 2 3 4 5 6 7
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.ReportViewer.WinForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 系统找不到指定的文件。 File name: 'Microsoft.ReportViewer.WinForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at SupSystem.fr_Print..ctor()
(这段代码位于Src_exp2_3.java中) public static String getValue(String key) throws IOException{ Properties pro = new Properties(); // here FileReader in = new FileReader("pro.txt"); pro.load(in); in.close();