0

I am trying to upload file to Unix directory from java class and getting FileNotFoundException on upload and I can't see what the problem is. To upload file I am using jcraft API and error happens on this command "channelSftp.put(new FileInputStream(f), f.getName()); ". File exists, connections are working and parameters(fileName and pathToUpload are being passed correctly. Is the error because fileName directory path isn't attached with it? Browser wouldn't let me send path with it, just file name. I'll post my code if anyone have a clear solution Please post it here. Sample code would be really helpful. Thanks all.

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;
}

Stack Trace:

     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)
pinkpanther
  • 4,770
  • 2
  • 38
  • 62
JS11
  • 181
  • 3
  • 7
  • 16
  • Please post the full stack trace. – thegrinner Jun 10 '13 at 15:36
  • Are your sure that your file is in the Java working directory, as if you only use the filename without a path, your source file has to be located where your Java program has been launched. Try also to avoid using method parameters and locale variables having the same name (`fileName`). – gma Jun 10 '13 at 15:37
  • this is the full stacktrace – JS11 Jun 10 '13 at 15:44
  • 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.(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) – JS11 Jun 10 '13 at 15:45
  • what it the value you're passing fileName parameter in uploadFile method. you can use CacheDirectory in like this http://stackoverflow.com/a/6349454/388053 – Muhammad Saifuddin Jun 10 '13 at 15:46
  • I am attaching file from the download folder of my local drive – JS11 Jun 10 '13 at 15:47
  • fileName parm will have something like document.pdf and pathToUpload parm will have unix dir path like /home/username/folder – JS11 Jun 10 '13 at 15:49
  • gma I am attaching file from my local directory like from desktop or wherever user stores the file to attach. – JS11 Jun 10 '13 at 15:54
  • I am attaching file from local directory using browse control on my JSP page but browser won't allow me to pass directory just a file name. How do I include path to find this file if browser won't let me do it? – JS11 Jun 10 '13 at 16:34
  • Anyone? I am sure many of you faced this issue at some point. Help a newbe – JS11 Jun 10 '13 at 16:43
  • @JS11 have you see the link i shared do specify the filename with full path. – Muhammad Saifuddin Jun 10 '13 at 16:51
  • Saifuddin - yes, I looked at the link. I don't entirely understand that solution. Is getCacheDirectory() a way get a location of the file? Not sure how to implement it. Would you be able to explain it a bit? – JS11 Jun 10 '13 at 17:07
  • try print f.getCanonicalPath() before channelSftp.put line.. and see what it prints? – Muhammad Saifuddin Jun 10 '13 at 20:01

1 Answers1

0

just work in this way would be fine:

  1. create an empty file to the right location like below example;

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

  2. write the file with FileOutPutStream:

    FileOutputStream fos = new FileOutputStream(file);
    byte[] bytes = new byte[1024];
    int length;
    while ((length = is.read(bytes)) >= 0) {
        fos.write(bytes, 0, length);
    }
    
zappee
  • 20,148
  • 14
  • 73
  • 129
liu baijun
  • 11
  • 2