• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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
0
cutie_smily
Asked:
cutie_smily
  • 12
  • 7
  • 4
  • +2
4 Solutions
 
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?
0
 
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
0
 
cutie_smilyAuthor Commented:
My class file is not in the same directory where my jsp and png files are..
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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" )
0
 
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.
0
 
cutie_smilyAuthor Commented:
I am using JBoss in linux
how do i give the file seperator ..is it the same way as in windows
0
 
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> how do i give the file seperator

Use /
0
 
cutie_smilyAuthor Commented:


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

Almost midnight and I'm leaving office for the day now, see you tomorrow.
0
 
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!

0
 
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.
0
 
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.
0
 
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!
0
 
CEHJCommented:
Try the following:


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

Put the image in your package root
0
 
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      
0
 
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                          
0
 
CEHJCommented:
Please post exact code you're using
0
 
objectsCommented:
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"));
0
 
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+"]");
            
%>
0
 
objectsCommented:
where is your image?
0
 
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.
0
 
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.
0
 
objectsCommented:
did u try what I suggested above?

what directory is your web application in?  Is the image inside your webapp?
0
 
cutie_smilyAuthor Commented:
I am not sure how to use aaplication.gerRealPath. my image file in /data and /my prgram in /web
0
 
objectsCommented:
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
0
 
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.
0
 
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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 12
  • 7
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now