File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Upload Image File Tomcat - File Path Not read in UNIX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Upload Image File Tomcat - File Path Not read in UNIX" Watch "Upload Image File Tomcat - File Path Not read in UNIX" New topic
Author

Upload Image File Tomcat - File Path Not read in UNIX

Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
the following code works from eclipse in local tomcat machine in windows

String prefix = "C:\\image\\";

File file = new File(prefix+item.getName());

logger.info("Image- Size0 ="+file.length());

the above code give length of file in windows tomcat environment but when i upload the war file in server in tomcat in unix/lenix environment the file size = 0

please help why in unix linux tomcat the file path is not read ==> String prefix = "C:\\image\\";

please help
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

If this is for a file upload, see this topic: http://www.coderanch.com/t/609232/Servlets/java/find-uploaded-file-upload You cannot just use normal file I/O.

If not, please elaborate what you are trying to accomplish.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
Please i need urgent hep, i tried all articles from google to solve this simple issue but nothing is working

i am only trying to read a simple image file from C drive in the image directory C://image but unable to create a file from server , it works in the windows env but does not work when war file is uploaded on server.

please need help my projret dead line is close but no solution is working.

String prefix = "C:\\image\\";
String imageName = "image.gif";

File file = new File(prefix+imageName);

Unable to make file from above code, please guide me what i am doing wrong , it works in the windows environment but says file not found when uploaded to tomcat server on the internet.

Thanks
Tim Cooke
Rancher

Joined: Mar 28, 2008
Posts: 526
    
  23

You are using a Windows platform specific file path in your code which is why it is not working. The path "C:\image\image.gif" is not a valid file path on a Unix/Linux platform.


Tim Driven Development
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
please guide me what is right path to get file path in UNIX

i tried all the following but nothing works , when uploaded war file in unix lenix it says file not found..

File image = new File("C:\\image\\image.jpg");


File image = new File("C:/image/image.jpg");

none of the above is working please guide what file path is correct in unix, i am nor able to create file it says file not found in unix
i am selecting a file from the local machine and upload file on UNIX tomcat server, it works for windows tomcat but does not work in UNIX tomcat


thanks
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
I am geting following error from UNIX tomcat server log file


java.io.FileNotFoundException: C:\image\ali-pic.jpg (No such file or directory)

java.io.FileNotFoundException : C:\image\ali-pic.jpg (No such file or directory)
exception.getLocalizedMessage: C:\image\ali-pic.jpg (No such file or directory)
exception.getMessage......... : C:\image\ali-pic.jpg (No such file or directory)


same code works in windows tomcat env but when uploaded to unix server tomcat gives above error
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

Why are you trying to read the file with file I/O in the first place?

Why isn't this file already part of the web application?

Why is the file in a folder external to the web app?

Is this file expected to be on the server or on the client?

Just reposting the same incorrect code again and again is not going to get you anywhere. Answer the questions.

Your topic says file upload, but as already pointed out, file uploading is not implemented using File I/O.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

Syed Ali wrote: exception.getMessage......... : C:\image\ali-pic.jpg (No such file or directory)
same code works in windows tomcat env but when uploaded to unix server tomcat gives above error

Do you know anything about the Unix directory hierarchy?.


SCJP, SCWCD.
|Asking Good Questions|
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
I like to explain what i am trying to achieve from this one line of code, i have a web application written in java-spring-hibernate-mysql.
The users of my application upload their image in a MySql database table by selecting their image from their local C: Drive image directory ( C://image//image.gif ),
After selecting image thuru dialog box, my app upload the user image into MySql database in the image table.
All My code is working perfectly except this one line of code where i have to construct the FILE from local C://image directory.

The image file can not be part of the web application, it has to be selected externally from the C: drive of local machines of different users .
I can not paste here all my code for clarity reason, if the this one line of code works then all my code will function correctly from selecting image to saving in the database table.
I am only stuck in this one line of simple code which does works perfectly when runs under windows env using local tomcat - eclipse but gives error when war file is uploaded to online UNIX server using tomcat

java.io.FileNotFoundException: C:\image\ali-pic.jpg (No such file or directory)
java.io.FileNotFoundException : C:\image\ali-pic.jpg (No such file or directory)
exception.getLocalizedMessage: C:\image\ali-pic.jpg (No such file or directory)
exception.getMessage......... : C:\image\ali-pic.jpg (No such file or directory)

I checked all online google articles, they all recommend using the following line but it does not work when uploaded to unix server

File image = new File("C:\\image\\image.jpg");


The Following is the code i am using to construct the file object from the selected image.

byte[] bFile = null;
Image image = new Image();

String prefix = "C:\\image\\";

File file = new File(prefix+item.getName()); // item.getName() will get the image file name from local drive

bFile = new byte[(int) file.length()];

try {
FileInputStream fileInputStream = new FileInputStream(file);
//convert file into array of bytes
fileInputStream.read(bFile);
fileInputStream.close();

} catch (Exception exception) {
logger.info("*** exception-start ***");
logger.info("*** exception.getLocalizedMessage: "+exception.getLocalizedMessage());
logger.info("*** exception.getMessage.........: "+exception.getMessage());
logger.info("*** exception-end ***");
}
}
}


image.setImage(bFile);
return image;

If the above code works in the unix env like windows will solve my issue of uploading the image from local c drive to online mysql database table.
The images can not be part of the application , they have to be selected from local drive and construct the FILE object, in order to construct the file object i need to provide the local path where the image is located on c drive.
I hope i explained well my situation, as i said all my code is working perfectly if the above line works in unix ENV.

please help my project deadline is close.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

Syed Ali wrote:The image file can not be part of the web application, it has to be selected externally from the C: drive of local machines of different users .


And this is the part that we have been trying to tell you for days that you cannot do using file I/O. You can't. Never. Ever. Not gonna happen!

Did that get through?

Your server code cannot, and never will be able to, read a file from the client machine. Not possible. No how. No way. Period. Could you imagaine the security nightmare if it were possible for any system to simply read any file off another system?

Is that clear?

To upload a file from the client machine to the server you must use HTTP and a multi-part request as explained to you in the very first reply to this topic. See http://www.coderanch.com/t/609232/Servlets/java/find-uploaded-file-upload for information and a pointer on how to find more info on how to perform a file upload.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

P.S. During the file upload, you cannot dictate where the users have the image on their disk. They will select the file from a dialog box presented by the browser.
Tim Cooke
Rancher

Joined: Mar 28, 2008
Posts: 526
    
  23

Right ok I see what your problem is now. That's the good news. The bad news is that you're doing it all wrong. Here's the problem as I understand it.

When you are running your application in the Tomcat server on your local Windows machine your code, which is doing basic File IO, is able to get hold of an image file that you have stored on your machine at "C:\image\image.gif".
But when you upload your application to a remote UNIX Tomcat server you are no longer able to access the file on your local machine.

Does this sound about right?

Using File IO to get hold of the image file is not the same as uploading it through your web application. This looks like it is working locally for you as a side effect of the fact that your Tomcat server is running on your local machine and thus has access to your file system. When you are running your application on the remote Tomcat server then your code will be looking for the file on the server file system not your local file system. The fact that the remote server is Unix is totally irrelevant at this point. A remote Windows server will still give you the same problem.

You need to implement file upload using a HTML Form backed by a Servlet. An example of this can be found in the link that Bear provided yesterday. http://www.coderanch.com/t/609232/Servlets/java/find-uploaded-file-upload
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
I tried using the article you mentioned but still do not work, it still says file not found

String absolutePath = getServletContext().getRealPath(item.getName());

the above line in that article still gives file not found error on unix server

please advise

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

Seems like you are putting the cart before the horse. Have you implemented the file upload? Which library did you use or are you using the Servlets 3 upload? Can you see the file on the server file system after the upload?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

Here is the direct link to the FAQ entry. You want to read the HTTP section.
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
I just tried using the exact code from the article you mentioned , http://www.coderanch.com/t/609232/Servlets/java/find-uploaded-file-upload
The code not even works in the windows environment it says file not found
I am using exact same code is a copy past from the article doing minor changes to fit in my application, but the main code is exact same. i am getting following error even in windows environment

i am using the following libraries same as article mentioned
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;


The following is the out put from the FileUploadServlet mentioned in the article when ran in windows env

Filname is:ali-pic.jpg

Path name:F:\Projects\ProjectV9.3\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\PtiMvc\ali-pic.jpg

12:08:28,376 [http-8080-3] INFO ImageUploadServlet - *** exception-start ***
12:08:28,376 [http-8080-3] INFO ImageUploadServlet - *** exception.getStackTrace()....: [Ljava.lang.StackTraceElement;@1a1732d
12:08:28,376 [http-8080-3] INFO ImageUploadServlet - *** exception.toString...........: java.io.FileNotFoundException: F:\Projects\ProjectV9.3\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\PtiMvc\ali-pic.jpg\ali-pic.jpg (The system cannot find the path specified)
12:08:28,376 [http-8080-3] INFO ImageUploadServlet - *** exception....................: java.io.FileNotFoundException: F:\Projects\ProjectV9.3\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\PtiMvc\ali-pic.jpg\ali-pic.jpg (The system cannot find the path specified)
12:08:28,376 [http-8080-3] INFO ImageUploadServlet - *** exception.getLocalizedMessage: F:\Projects\ProjectV9.3\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\PtiMvc\ali-pic.jpg\ali-pic.jpg (The system cannot find the path specified)
12:08:28,376 [http-8080-3] INFO ImageUploadServlet - *** exception.getMessage.........: F:\Projects\ProjectV9.3\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\PtiMvc\ali-pic.jpg\ali-pic.jpg (The system cannot find the path specified)
java.io.FileNotFoundException: F:\Projects\ProjectV9.3\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\PtiMvc\ali-pic.jpg\ali-pic.jpg (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at com.pti.systems.service.ImageUploadServlet.doPost(ImageUploadServlet.java:87)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
The following is the code used to construct the FILE object from the article http://www.coderanch.com/t/609232/Servlets/java/find-uploaded-file-upload

PrintWriter out = response.getWriter();
String absolutePath = getServletContext().getRealPath(item.getName());
String fileName = item.getName();

File uploadedFile = new File(absolutePath + "/" + fileName);

The above line throws exception even in windows env saying file not found, i pasted the exception in my last post

please advise .... Thanks
Syed Ali
Greenhorn

Joined: Oct 08, 2008
Posts: 29
In my original code i have a ImageManager class which process the image selected by the user from the file dialog box, i am using apache commons file upload API.
The following is the list of API i am importing in my class

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

The following is the main code to process image selected from the Crive

String prefix = "C:\\image\\";

File file = new File(prefix+item.getName());

bFile = new byte[(int) file.length()];

try {
FileInputStream fileInputStream = new FileInputStream(file);
//convert file into array of bytes
fileInputStream.read(bFile);
fileInputStream.close();
} catch (Exception exception) {
logger.info("*** exception-start ***");
logger.info("*** exception.getLocalizedMessage: "+exception.getLocalizedMessage());
logger.info("*** exception.getMessage.........: "+exception.getMessage());
logger.info("*** exception-end ***");
}
}
}

image.setImage(bFile);

return image;

The above code works in the windows env but the code from the article does not even works in the windows env.. please advise ... thanks
Tim Cooke
Rancher

Joined: Mar 28, 2008
Posts: 526
    
  23

Your code is still trying to read the image file from the server's local file system rather than the image you've just uploaded through the web form.

String prefix = "C:\\image\\";

File file = new File(prefix+item.getName());

bFile = new byte[(int) file.length()];

The variable item is a FileItem that "represents a file or form item that was received within a multipart/form-data POST request". Instead of asking it for it's name and then trying to look it up on the wrong system, which is what you're doing, you need to ask it for the file contents. Take a look at Using FileUpload to see your options. You can read the file contents into memory as a byte[] or you can get a handle on an InputStream and save it to a file on your server.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 294
    
    2

Sayed,

I just read this thread.

Do you understand the the fundamental problem with what you're trying to do and why it works from Eclipse on your local machine but not on the server?

It's been stated a couple of times but I'm not sure you understand it. Before I attempt to restate what Bear and Tim said on a simple conceptual level, I'd like to know if you need such an intro to file uploading.

Best,
Joe


It's not what your program can do, it's what your users do with the program.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Upload Image File Tomcat - File Path Not read in UNIX
 
Similar Threads
creating file relative to root of web application in windows OS
Reading file both with absolute path and relative path
Uploading Image To the server
Getting file name from a directory path
Java policy file related problems