thuliobuarque / java2word

Automatically exported from code.google.com/p/java2word
0 stars 0 forks source link

Unable to Insert Image in Word Doc #66

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I am attaching the source code, please check and let me know why I am getting 
the following error :
java.lang.RuntimeException: Can't create ImageIO. Maybe the path is not valid. 
Path: 
http://www.w3.org/html/logo/downloads/HTML5_Logo_256.png
ImageLocation: WEB_URL
    at word.w2004.elements.Image.<init>(Image.java:48)
    at word.w2004.elements.Image.from_WEB_URL(Image.java:147)
    at InsertImage.main(InsertImage.java:36)
Caused by: javax.imageio.IIOException: Can't get input stream from URL!
    at javax.imageio.ImageIO.read(ImageIO.java:1369)
    at word.w2004.elements.Image.<init>(Image.java:41)
    ... 2 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:518)
    at java.net.Socket.connect(Socket.java:468)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:389)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:516)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:318)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:792)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:733)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:658)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:981)
    at java.net.URL.openStream(URL.java:1009)
    at javax.imageio.ImageIO.read(ImageIO.java:1367)
    ... 3 more

Please rectify me as soon as possible.

Waiting for reply.

Regards

Original issue reported on code.google.com by ankur881...@gmail.com on 11 Oct 2011 at 12:24

Attachments:

GoogleCodeExporter commented 8 years ago
I have seen this same issues many times... are you under a proxy? 

Original comment by leonardo...@gmail.com on 12 Oct 2011 at 12:27

GoogleCodeExporter commented 8 years ago
Even on removing the proxy, I am getting the same error.
Also on using "Image.from_FULL_LOCAL_PATHL(Utils.getAppRoot()", I am getting 
the error.

Please help me in getting the solution.
Waiting for reply.

Regards

Original comment by ankur881...@gmail.com on 12 Oct 2011 at 4:02

GoogleCodeExporter commented 8 years ago
In order to prove that works, try some image inside your intranet network, or 
some image in your local tomcat "localhost:8080/myimage.png"

Btw "java.net.ConnectException: Connection refused: connect" has nothing to do 
with java2word. It could be firewall, proxy...

Image works fine except under proxy but this is environment issue. Can you do 
the first  thing just to prove that works?

Original comment by leonardo...@gmail.com on 12 Oct 2011 at 4:26

GoogleCodeExporter commented 8 years ago
After implementing your change, I have this piece of code:
import java.io.File;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import word.api.interfaces.IDocument;
import word.w2004.Document2004;
import word.w2004.Document2004.Encoding;
import word.w2004.elements.BreakLine;
import word.w2004.elements.Heading1;
import word.w2004.elements.Image;

import com.nated.framework.exception.NatEdException;

public class InsertImage {

    static Logger logger = Logger.getLogger(InsertImage.class.getName());

    public static void main (String[] args){

        IDocument myDoc = null;

        File newDocument = new File("C:\\Insert_Image.doc");
        PrintWriter writer = null;
        try {
            logger.log(Level.INFO, "main() starts");

            writer = new PrintWriter(newDocument); 
            myDoc = new Document2004();
            myDoc.encoding(Encoding.UTF_8);
            myDoc.addEle(BreakLine.times(1).create());
            myDoc.addEle(Heading1.with("Insert Image Details").create());

             //myDoc.getBody().addEle(Utils.getAppRoot() + "\\wss.jpg");
            myDoc.addEle(Image.from_WEB_URL("http://localhost/apache_pb.png").setHeight("40").setWidth("80").create());

            String text = myDoc.getContent();
            writer.println(text);
            writer.close();

        }catch (Exception exception) {
            if (!(exception instanceof NatEdException)) {
                exception.printStackTrace();
            }
        } finally {
            logger.log(Level.INFO, "main() ends");
        }
    }
}
------------------------------------------------------------------------------
But Now I have this error:
Exception in thread "main" java.lang.NoClassDefFoundError: 
com/thoughtworks/xstream/core/util/Base64Encoder
    at word.utils.ImageUtils.getImageHexaBase64(ImageUtils.java:26)
    at word.w2004.elements.Image.getContent(Image.java:89)
    at word.w2004.Body2004.addEle(Body2004.java:16)
    at word.w2004.Document2004.addEle(Document2004.java:110)
    at InsertImage.main(InsertImage.java:37)
---------------------------------------------------------------------
Please let me know where I am wrong. This error is also getting generated on 
using Image from local path.

Original comment by ankur881...@gmail.com on 12 Oct 2011 at 4:43

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
this one is easy: http://code.google.com/p/java2word/#WITHOUT_Maven?
You need xstream in your classpath.

See if now you can see the image on the doc

cheers
Leonardo

Original comment by leonardo...@gmail.com on 12 Oct 2011 at 6:05

GoogleCodeExporter commented 8 years ago
Thanks for the solution, now its working fine with images in the range of 
localhost.
But still the connection error persist with outside localhost images, even on 
removing the proxy.
Please help me in this as it is very necessary to get images outside the 
localhost.
Also specify your way of removing proxy, may be I am wrong in that too.

Waiting for reply.

Regards 

Original comment by ankur881...@gmail.com on 12 Oct 2011 at 7:20

GoogleCodeExporter commented 8 years ago
maybe try this at the beginning of the method: 

System.setProperty("http.proxyHost", "your proxy here")
System.setProperty("http.proxyHost", "8080")

I never tried but this is how you set up proxies in Java. 

It definitely works when you are directly connected in the internet. 
If you find out how to set up proxy, I can add to the library... something like:

myDocument.setProxyHost();

then everything inside the document that does network will use the proxy 
settings... 

give it a try. 

Leonardo

Original comment by leonardo...@gmail.com on 12 Oct 2011 at 10:37

GoogleCodeExporter commented 8 years ago

Original comment by leonardo...@gmail.com on 2 Feb 2012 at 12:25

GoogleCodeExporter commented 8 years ago
HI

bellow is the code i am trying to execute. but iam getting 

Exception in thread "Main Thread" java.lang.NoClassDefFoundError: 
com/thoughtworks/xstream/core/util/Base64Encoder
    at word.utils.ImageUtils.getImageHexaBase64(ImageUtils.java:26)
    at word.w2004.elements.Image.getContent(Image.java:88)
    at WordImage.main(WordImage.java:37)

Kindly help me in sorting out the problem

Original comment by sirisha....@gmail.com on 27 Jul 2012 at 12:39

Attachments: