【问题标题】:FileNotFoundException when uploading file to Unix from Java [duplicate]从 Java 将文件上传到 Unix 时出现 FileNotFoundException [重复]
【发布时间】:2013-06-10 15:32:32
【问题描述】:

我正在尝试将文件从 java 类上传到 Unix 目录,并在上传时收到 FileNotFoundException,但我看不出问题出在哪里。要上传文件,我正在使用 jcraft API 并且此命令发生错误“channelSftp.put(new FileInputStream(f), f.getName()); “。文件存在,连接正在工作并且参数(fileName 和 pathToUpload 正在正确传递。错误是因为 fileName 目录路径没有附加吗?浏览器不会让我发送路径,只是文件名。我'如果有人有明确的解决方案,我会发布我的代码请在此处发布。示例代码将非常有帮助。谢谢大家。

public String uploadFile(String fileName, String pathToUpload) throws IOException {
    session = UnixConnect.getInstance();
    String SFTPWORKINGDIR = pathToUpload;
    String result ="File failed to upload";
    String fileName = new File(fileName).getName(); // file is document.pdf 

    Channel channel = null;
    ChannelSftp channelSftp = null;

    try {
        channel = session.openChannel("sftp");
        channel.connect();
        //System.out.println("SFTP connection established");
        channelSftp = (ChannelSftp)channel;
        channelSftp.cd(SFTPWORKINGDIR);

        File f = new File(fileName);
        ////////////////////////////////////////////
        // file not found error in the next line. 
        //////////////////////////////////////////
        channelSftp.put(new FileInputStream(f), f.getName());


        //change mode for uploaded file 
        String fullpath = SFTPWORKINGDIR +  fileName;
        channel=session.openChannel("exec");
        ((ChannelExec)channel).setCommand("chmod 770 " + fullpath);
        channel.setInputStream(null);
        ((ChannelExec)channel).setErrStream(System.err);

        InputStream in=channel.getInputStream();
        channel.connect();

        result = "File " + fileName + " updloaded to directory " + SFTPWORKINGDIR;

    }
    catch (Exception e) {
        System.out.println("Class uploadFile exception: " + e.toString());  
    }
    finally{
         if (channel != null) {
             channel.disconnect();
         }
    }

    return result;
}

堆栈跟踪:

     08:42:02,583 ERROR [STDERR] java.io.FileNotFoundException: test.pdf 
        (The system cannot find the file specified) 08:42:02,583 ERROR [STDERR] at 
java.io.FileInputStream.open(Native Method) 08:42:02,584 ERROR [STDERR] at 
    java.io.FileInputStream.<init>(FileInputStream.java:120) 08:42:02,584 ERROR [STDERR] at 
    spt.implement.uploadFile.uploadFile(uploadFile.java:49) 08:42:02,584 ERROR [STDERR] at 
    spt.controller.UploadController.doPost(UploadController.java:35) 08:42:02,584 ERROR 
    [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)

【问题讨论】:

  • 请发布完整的堆栈跟踪。
  • 您确定您的文件在Java 工作目录中,就像您只使用没有路径的文件名一样,您的源文件必须位于您的Java 程序启动的位置。还要尽量避免使用同名的方法参数和语言环境变量 (fileName)。
  • 这是完整的堆栈跟踪
  • 08:42:02,583 ERROR [STDERR] java.io.FileNotFoundException: test.pdf (系统找不到指定的文件) 08:42:02,583 ERROR [STDERR] at java.io.FileInputStream .open(Native Method) 08:42:02,584 错误 [STDERR] 在 java.io.FileInputStream.(FileInputStream.java:120) 08:42:02,584 错误 [STDERR] 在 spt.implement.uploadFile.uploadFile( uploadFile.java:49) 08:42:02,584 错误 [STDERR] 在 spt.controller.UploadController.doPost(UploadController.java:35) 08:42:02,584 错误 [STDERR] 在 javax.servlet.http.HttpServlet.service( HttpServlet.java:637)
  • 您在uploadFile 方法中传递fileName 参数的值是什么。你可以像这样使用 CacheDirectory stackoverflow.com/a/6349454/388053

标签: java unix servlets


【解决方案1】:

以这种方式工作就可以了:

  1. 在正确的位置创建一个空文件,如下例所示;

    channelSftp.put( new ByteArrayInputStream( "".getBytes() ), 'folder/folder/file.txt');

  2. 使用 FileOutPutStream 写入文件:

    FileOutputStream fos = new FileOutputStream(file);
    byte[] bytes = new byte[1024];
    int length;
    while ((length = is.read(bytes)) >= 0) {
        fos.write(bytes, 0, length);
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多