输入文件进行传输时出错

用户名

我的任务是通过发送文件socket我在提示要发送多少个文件。但是它总是在抛出exception

这是代码:

public void soc_server() throws IOException {
    servsock = new ServerSocket(55000);
    sock = servsock.accept();
    System.out.println("Hello Server");
    Scanner sc = new Scanner(System.in);
    **System.out.println("how many files to be sent: ");
    String temp = sc.nextLine();
    // i used to take integer here. Still it was throwing same exception.
    int fileNumber = Integer.parseInt(temp);
    sc.close();
    sc = new Scanner(System.in);**
    for (int x = 0; x < fileNumber; x++) {
        System.out.println("Please enter the file name or file path ");
        String s = sc.nextLine();
        File file = new File(s);
        if (file.exists())
            System.out.println("File found");
        else
            System.out.println("File not found");
        OutputStream out = sock.getOutputStream();
        fileInputStream = new FileInputStream(s);
        byte[] buffer = new byte[100 * 1024];
        int bytesRead = 0;
        System.out.println("sending file no: " + x);
        while ((bytesRead = fileInputStream.read(buffer)) != -1) {
            if (bytesRead > 0) {
                out.write(buffer, 0, bytesRead);
                totalSent += bytesRead;
                System.out.println("sent " + (totalSent / 1024) + " KB "
                        + ((System.currentTimeMillis() - time) / 1000)
                        + " sec");
            }
        }
        out.flush();
        fileInputStream.close();
        // sock.close();
        out.close();
        // servsock.close();
        // servsock = new ServerSocket(55000);
        // sock = servsock.accept();
        pw = new PrintWriter(sock.getOutputStream(), true);
        pw.print(s);
        pw.flush();
        pw.close();
    }
    sock.close();
    servsock.close();
    sc.close();
    System.out.println("Sent " + (totalSent / 1024) + " kilobytes in "
            + ((System.currentTimeMillis() - time) / 1000) + " seconds");
}

例外:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Server.soc_server(Server.java:41)
at Index.main(Index.java:21)

除了要发送多少文件的提示行外,其余代码都可以正常工作。我对在写作时为什么会遇到同样的异常感到非常困惑:

String temp = sc.nextLine();
int fileNumber = sc.nextInt();

谢谢你。

萨克

在代码开头分配扫描程序:删除以下行

    sc.close();
    sc = new Scanner(System.in);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel 4 - 上传输入文件时出错

来自分类Dev

尝试对文件求和并通过管道/叉/过程进行传输时出错?

来自分类Dev

使用文件输入上传文件时出错

来自分类Dev

部署工件时出错:传输文件失败返回码是:401

来自分类Dev

通过套接字传输多个文件时出错

来自分类Dev

传输文件时出错:连接超时:在Maven中连接。

来自分类Dev

输入Excel时出错

来自分类Dev

远程将pdf文件流传输到Solr中时出错

来自分类Dev

使用Boto将文件从ec2传输到s3时出错

来自分类Dev

bash:计算文件数量并进行比较时出错

来自分类Dev

在布局文件中对MapView进行充气时出错

来自分类Dev

在布局文件中对MapView进行充气时出错

来自分类Dev

出错时停止GCC,进行多个文件编译

来自分类Dev

编辑页面时将文件上传输入留空以保留文件?

来自分类Dev

通过网络中的Socket进行文件传输时,此程序中的文件传输错误是什么?

来自分类Dev

使用 source_transaction 传输时出错

来自分类Dev

在Dropbox文件夹中使用encfs文件夹进行“输入/输出错误”

来自分类Dev

在Julia中流式传输数据时从文本文件解析最后一个位置值时出错

来自分类Dev

将输入与文件信息进行比较时出现问题

来自分类Dev

删除文件时出错

来自分类Dev

移动文件时出错

来自分类Dev

从文件读取时出错

来自分类Dev

移动文件时出错

来自分类Dev

删除文件时出错

来自分类Dev

写入文件时出错

来自分类Dev

保险丝文件系统-访问Office文件时出现常规输入/输出错误

来自分类Dev

JavaScript和文件上传输入字段

来自分类Dev

C ++文件输入,流式传输和编译

来自分类Dev

对行号进行分区时出错

Related 相关文章

热门标签

归档