SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reference Dir on Server

    Hello

    I am working thru an issue with my hosting company and they made a statement that is not making sense to me. I am hoping someone can help.

    What I am trying to do is write a file to a folder on a shared hosting Linux server using Java. To get the root location so I know where to write the file to, I use this code:

    Code:
    String filePath = getServletContext().getRealPath( "/" ) + "tmp" + "/" + saveFile;
    this returns me a location but I am being told I do not have permission to access this location in this manner. Instead, I am suppose to code it like this (or at least this is how I am understanding what they are telling me..

    Code:
    String filePath = "/" + "tmp" + "/" + saveFile;
    The method they described to me does not seem like it will work because it is more of a relative path instead of providing the whole path.

    My question to anyone who can help is, is there another way to provide the location to a directory instead of using getServletContext().getRealPath( "/" )? Is there a relative path way from a servlet?

    Here is ultimately what I am trying to do.

    Code:
    // creating a new file with the same name and writing the content in new file
    FileOutputStream fileOut = new FileOutputStream( getServletContext().getRealPath( "/" ) + "tmp" + "/" + saveFile );
    Thank you in advance for any help you can provide.

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My question to anyone who can help is, is there another way to provide the location to a directory instead of using getServletContext().getRealPath( "/" )?
    Not that I know of.

    However the way they suggested it (which is relative) will be relative from wherever the JVM process is called. So I'm not sure how the host has it set up but if it's a shared JVM I'm not really sure how that's supposed to help you. But in terms of testing you should try this.

    Code:
    File file = new File("");
    System.out.println(file.getAbsolutePath());
    Of course if you're using log4j or some other logging API that would be better, but basically you can use this to test where it would generate the file. You can also do like "file.canWrite()" to see if it's writable there.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for the feedback. I will give this a try. I have setup my own custom logging since log4j is not an option on shared hosting.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the long delay on my response. Not surprisingly, I had more server issues. I made the code change you advised with the new file to try and determine the path. I installed the code and had to wait until 1:00 am for a server restart. The change did not take effect. So I thought maybe I had done something wrong, so I made a 2nd change to verify. This did not work either. So for the 26th time, I called the help desk and they acknowledge that there was a server issue. I tell you this just to explain why there is a delay.

    Here is the code I put in:

    Code:
    try {
    File fileNew = new File( "" );
    	this.log( "absolute path: " + fileNew.getAbsolutePath() );
    } catch( Exception e ) {
    	this.log("exception getting absolute path: " + e.toString() );
    }
    And here is what the output was:

    absolute path: /

    I will be honest, this seems to be a relative path to me.

    So for tonight I am going to install some code to copy the file such as this:

    FileOutputStream fileOut = new FileOutputStream( "/" + "tmp" + "/" + saveFile );
    fileOut.write( dataBytes, startPos, ( endPos - startPos ) );
    fileOut.flush();
    fileOut.close();

    I will post my results. If you have any feedback on what the single / path means to you, that would be appreciated.

    Thanks again for your time and knowledge.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OKů I have some good news and some not so good news.

    The good news is, the security manager says I have permission. Here is how I verified:

    Code:
    SecurityManager security = System.getSecurityManager();
    String filePath = "/" + "tmp" + "/" + saveFile;
    security.checkWrite( filePath );
    this.log("Has Permission with forward slash!!!" );
    The not so good news is this code executed without throwing an exception but when I FTPed into the server, I do not see the file.

    Here is the code that writes the file for me.

    Code:
    DataInputStream in = new DataInputStream( req.getInputStream() );
    
    //we are taking the length of Content type data
    int formDataLength = req.getContentLength();
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    
    //this loop converting the uploaded file into byte code
    while( totalBytesRead < formDataLength ) 
    {
    	byteRead = in.read( dataBytes, totalBytesRead, formDataLength );
    	totalBytesRead += byteRead;
    }
    
    String file = new String(dataBytes);
    
    String saveFile = file.substring( file.indexOf("filename=\"") + 10 );
    saveFile = saveFile.substring( 0, saveFile.indexOf( "\n" ) );
    saveFile = saveFile.substring( saveFile.lastIndexOf( "\\" ) + 1,saveFile.indexOf( "\"" ) );
    int lastIndex = contentType.lastIndexOf( "=" );
    String boundary = contentType.substring( lastIndex + 1, contentType.length() );
    int pos;
    
    //extracting the index of file 
    pos = file.indexOf( "filename=\"" );
    pos = file.indexOf( "\n", pos ) + 1;
    pos = file.indexOf( "\n", pos ) + 1;
    pos = file.indexOf( "\n", pos ) + 1;
    int boundaryLocation = file.indexOf( boundary, pos ) - 4;
    int startPos = ( ( file.substring( 0, pos ) ).getBytes() ).length;
    int endPos = ( (file.substring( 0, boundaryLocation ) ).getBytes()).length;
    
    //writing file out
    try {
    	FileOutputStream fileOut = new FileOutputStream( "/" + "tmp" + "/" + saveFile );
    	this.log( "1" );
    	fileOut.write( dataBytes, startPos, ( endPos - startPos ) );
    	this.log( "2" );
    	fileOut.flush();
    	this.log( "3" );
    	fileOut.close();
    	this.log( "4" );
    } catch( Exception e ) {
    	this.log( "exception writing: " + e.toString() );
    }
    Would you have any idea why this code executes completely without throwing an exception but it does not write the file out? In the logs I see the 1 2 3 4 that I am logging.

    I know there are open source ways to uploading files but I have been told countless times by the hosting company that they do things that are not supported in a shared hosting environment. That is why I am trying to code this without using them.

    Thanks in advance for your time and knowledge.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •