I am getting Can't read input file!..can anyone tell me why



Here is how iam doing in my jsp file

 File file = new File("wakeupbase.png");
        if ( file.canRead() )
               System.out.println( "Can Read File: " + file.getName());


 BufferedImage image = ImageIO.read(file);




}catch(IOException ex){
            System.out.println("Error while constructing text message "
                        +ex.getMessage());
                           ex.printStackTrace();
            }

It throws an exception saying can't read an input file
cutie_smilyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
Where is the file wakeupbase.png? It should be contained in the same directory where you are running your application from. Can you tell what is the location of your class-file, what is the location of the wakeupbase.png file and where are you running it from?
cutie_smilyAuthor Commented:
png and jsp are in the same dir. I am not sure where my clas file is..i will fing out and let u know
cutie_smilyAuthor Commented:
My class file is not in the same directory where my jsp and png files are..
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Mayank SAssociate Director - Product EngineeringCommented:
Oh, sorry didn't notice it was a JSP. Try with a relative path for your image (w.r.t the web-server's root), like if your JSP has a URL "/webapp/folder/jsp1.jsp" then try "/webapp/folder/wakeupbase.png" )
Mayank SAssociate Director - Product EngineeringCommented:
>> My class file is not in the same directory where my jsp and png files are

The JSP will be compiled into a servlet by the servlet-container.
cutie_smilyAuthor Commented:
I am using JBoss in linux
how do i give the file seperator ..is it the same way as in windows
Mayank SAssociate Director - Product EngineeringCommented:
BTW, any reason why you need to read the image this way? You can just use an <img> tag in your JSP.
Mayank SAssociate Director - Product EngineeringCommented:
>> how do i give the file seperator

Use /
cutie_smilyAuthor Commented:


Iam reading background image writing a text on to it. (text is dynamically gerated)
Mayank SAssociate Director - Product EngineeringCommented:
Ok, try http:#16527561

Almost midnight and I'm leaving office for the day now, see you tomorrow.
cutie_smilyAuthor Commented:
mayankeagle

its finding png file but able to read it. see

File file = new File("wakeupbase.png");
        if ( file.canRead() )
               System.out.println( "Can Read File: " + file.getName());
else System.out.println( "Can't Read File: " + file.getName());



error:

Can't Read File: wakeupbase.png

2006-04-24 14:20:39,169 INFO  [STDOUT] Error while constructing text message Can't read input file!

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
File file = new File("/wakeupbase.png");
        if ( file.canRead() )
               System.out.println( "Can Read File: " + file.getName());


 BufferedImage image = ImageIO.read(file);
}catch(IOException ex){
          System.out.println("Error while constructing text message "
                    +ex.getMessage());
                           ex.printStackTrace();
}

Try using relative path as above that must solve it.

Thank You.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
http://javaalmanac.com/egs/javax.imageio/BasicImageRead.html?l=rel

check out the above link

  InputStream is = new BufferedInputStream(
            new FileInputStream("wakeupbase.png"));
        image = ImageIO.read(is);

try this and if you get a exception just put forward the stack trace.

Thank You.
cutie_smilyAuthor Commented:
It still says

2006-04-24 14:45:34,127 INFO  [STDOUT] Error while constructing text message Can't read input file!
CEHJCommented:
Try the following:


BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/wakeupbase.png"));

Put the image in your package root
cutie_smilyAuthor Commented:
CHEJ, Here it still not finding

2006-04-24 15:20:46,008 ERROR [org.jboss.web.localhost.Engine] StandardWrapperValve[phone.AirlineApp.confirminput_jsp]: Servlet.service() for servlet phone.AirlineApp.confirminput_jsp threw exception
java.io.FileNotFoundException: wakeupbase.png (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at phone.AirlineApp.confirminput_jsp._jspService(confirminput_jsp.java:131)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
/confirm      
cutie_smilyAuthor Commented:
Here is after giving relative path.. what is the below error mean

2006-04-24 15:39:03,462 INFO  [STDOUT] base dir:/airapp/data/blank.png

2006-04-24 15:39:03,464 ERROR [org.jboss.web.localhost.Engine] StandardWrapperValve[phone.AirlineApp.confirminput_jsp]: Servlet.service() for servlet phone.AirlineApp.confirminput_jsp threw exception
java.lang.IllegalArgumentException: input == null!
        at javax.imageio.ImageIO.read(ImageIO.java:1302)
        at phone.AirlineApp.confirminput_jsp._jspService(confirminput_jsp.java:153)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
/confirm                          
CEHJCommented:
Please post exact code you're using
Mick BarryJava DeveloperCommented:
use getRealPath() method to get the path to the image

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)

eg. if your image is stored in your webapp at /data/blank.png you would get the path using:

File image = new File(application.getRealPath("/data/blank.png"));
cutie_smilyAuthor Commented:
String baseFile = Global.dataDir + Global.fileSeparator + "blank.png";
     System.out.println("base dir:"+baseFile);    
       

        Font font = new Font("Times New Roman", Font.BOLD, 12);
                  String data=" Passenger Name: Bryan Armstrong.MR" +
                    " \nTicket Refernece : BSRCYF "+
                 "\nBooking Reference           BPU3YW "+
                 "\nIdentification             PP Z1445628 "+
                 "\nElectronic Ticket Reference    176 2105573696" +
                 "+\nIssued By          DUBAI / EK REVENUE";

      int rowHeight=22;
       Color color=Color.BLACK;
      Color bodycolor=Color.black;
      try{

               guestname=(String)session.getAttribute("gname");
               System.out.println("guestname:" +guestname);
                       

                         BufferedImage image = ImageIO.read(new File(baseFile));
                         Graphics graphics   = image.getGraphics();
                         graphics.setColor(color.brighter());
                         graphics.setFont(font);
                         Graphics2D g2 = image.createGraphics();
                         FontRenderContext fc= g2.getFontRenderContext();
                         Rectangle2D bounds  = font.getStringBounds(title,fc);
                         int width = (int) bounds.getWidth();
                         int xValue = (BASEIMAGE_WIDTH - width)/2;
                         int xValue1 = (BASEIMAGE_WIDTH - width)/12;
                         int i=1;
                         graphics.drawString(guestname, 10, (i++)*rowHeight);
                         StringTokenizer st = new StringTokenizer(data, "\n");
                         graphics.setColor(bodycolor.brighter());
                         while (st.hasMoreTokens())
                         {
                               String token = st.nextToken();
                               Vector textList = tokenizeStringIntoLines(token, font,
                                                             BASEIMAGE_WIDTH-20, fc);
                               for (int j=0; j<textList.size(); j++)
                               {
                                     graphics.drawString((String)textList.elementAt(j), 10,
                                                 (i++)*rowHeight);
                               }
                         }
                         // save modified image
                         ImageIO.write(image, "PNG", new File("test10.png"));
                  
      }catch(IOException ex){
            System.out.println("Error while constructing text message "
                        +ex.getMessage());
             System.out.println("can not read file");
              ex.printStackTrace();
                       StringWriter sw = new StringWriter();
                           PrintWriter pw = new PrintWriter(sw);
                           ex.printStackTrace(pw);
                           System.out.println("Exception is" +sw.toString());
   

            }



   // BackGroundImage1 bg=new BackGroundImage1();
   // bg.ImageMethod();
      System.out.println("genericinfo.jsp: URL ["+url+"]");
    System.out.println("genericinfo.jsp: EXIT URL ["+exitURL+"]");
            
%>
Mick BarryJava DeveloperCommented:
where is your image?
cutie_smilyAuthor Commented:
on my linux box .../ptc/data/

2006-04-24 15:53:35,828 INFO  [STDOUT] Exception isjavax.imageio.IIOException: Can't read input file!
        at javax.imageio.ImageIO.read(ImageIO.java:1263)
        at phone.AirlineApp.confirminput_jsp._jspService(confirminput_jsp.java:146)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
        at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:44)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:169)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.
cutie_smilyAuthor Commented:
i really need to fix as soon as possible. i have no help except here. i don't see y its not readin....if i write the code on windows it works fine.
Mick BarryJava DeveloperCommented:
did u try what I suggested above?

what directory is your web application in?  Is the image inside your webapp?
cutie_smilyAuthor Commented:
I am not sure how to use aaplication.gerRealPath. my image file in /data and /my prgram in /web
Mick BarryJava DeveloperCommented:
If its outside your wenb application then you'll need to supply the absolute path to the image file.
and check that the user that tomcat is running as has access rights to read the image file
Mayank SAssociate Director - Product EngineeringCommented:
>> my image file in /data and /my prgram in /web

Generally, you should keep all web-application images inside some directory contained in the root folder of the web-app.
CEHJCommented:
>>Generally, you should keep all web-application images inside some directory contained in the root folder of the web-app.

That's right. You can think of directories outside your webapp as off limits. And when you do so, you don't need to worry about its position in the file system - just let the classloader deal with it by using getResourceAsStream as i mentioned above
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.