We help IT Professionals succeed at work.

write a file on unix box

greenearth
greenearth asked
on
I have a java web application on weblogic which is on unix box. I am opening a file like this
 File temp = new File("test.bmp"); and writing an image into it in servlet A.

The file is been written at some default location. When I try to read the file back in servlet B i am getting an exception Exception: Can't read input file!

How do we store/read from unix  in java
Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:
I believ servle will write files to the root of your web application into webapps/thhis_application_name
Awarded 2011
Awarded 2011

Commented:
chec it there.
And you can spacify explisit path in you new File(...)
then you'll have no problems with that
Awarded 2011
Awarded 2011
Commented:


thises guys geive teh same recommednation - to use absolute path:
http://stackoverflow.com/questions/1086779/how-to-create-or-write-text-file-in-servlet

but still cehck on your applications server and there in webapps/app_nmae you have a chance to find it

Author

Commented:
does the explicit path relative to the root directory of the webapplication
can  you provide the code bit that you are currently using for Servlet A and Servlet B...it would be a lot easier to solve your problem!
Awarded 2011
Awarded 2011

Commented:
Seervlet is like any program ruinning on your unix box - so put the absolute path there - makes sure you have privuileges to write there
Awarded 2011
Awarded 2011

Commented:
It is the other way around - in acse you need then from the context ovject you can find the absolute path of your root.
But for this sirtuation you don;t need any of that - jus put there absolute path to your file as you see it on command line termianl on the same host
and wervlet wuill write there

Author

Commented:
Somehow the servelt is not able to display the image .. on browser it says image cannot be displayed as it contains errors

 PrintWriter out = res.getWriter();
 res.setContentType("image/jpeg");
File temp = new File("//opt//app//temp//test.bmp");
 BufferedImage image=  ImageIO.read(temp);
out.print(image);
Awarded 2011
Awarded 2011

Commented:
servlet can send the http page with the link to ithe imgae to the browser - but that is absolutely diofferent thing from waht we were talking about bfefore
Awarded 2011
Awarded 2011

Commented:
If you want to write the image and dispaly it to the lcien on the web page then you need to know the root of the documents of
of your web server - gthen you write to some folder under this root folder (and when you are wrtiting in the servlet the bmp file - you can use the
fiull path on the host to that folder) but the you should  place the link in yourt htto - and here it should have path in the link starting form your
root dociument folder - let me know if you undrstand that
Awarded 2011
Awarded 2011
Commented:
Say if your opath to the root folder of yur documents  of your web server is
/usr/apache/htdocs

then you would write file

 File temp = new File("/usr/apache/htdocs/images/test.bmp");
in your servlet code

but then owhen you cretae HTML page from your server you shoudl
put such link there

<img src="http://yourserver.com/images/test.bmp">


Author

Commented:
it might be one solution but I think the way I have done it should also be working ..but something is preventing it from displaying.
Awarded 2011
Awarded 2011

Commented:
I'm not sure you can do it the way you done it.
taht is how i alays send images to webpages is throuh link in the HTML page
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
 PrintWriter out = res.getWriter();
 res.setContentType("image/jpeg");
File temp = new File("//opt//app//temp//test.bmp");
 BufferedImage image=  ImageIO.read(temp);
out.print(image);
>>

That's not going to work and is functionally meaningless. If your objective is to display the image, then you need to do that in the normal way by including the correct path to it in an <img> tag
CERTIFIED EXPERT
Top Expert 2016

Commented:
otoh, if you want to *download* the image, that would be different again. What is your goal?

Author

Commented:
I had the following solution in end. my opjective was to just spit out the image on the browser
  ServletOutputStream sos = res.getOutputStream();
res.setContentType("image/jpeg");
 File temp = new File("//opt//app//test.bmp");
byte[] b = new byte[(int) temp.length()];
 FileInputStream fileInputStream = new FileInputStream(temp);
 fileInputStream.read(b);
 ByteArrayOutputStream baos = new ByteArrayOutputStream(b.length);
 baos.write(b);
 baos.writeTo(sos);
 sos.flush();

Explore More ContentExplore courses, solutions, and other research materials related to this topic.