Vardges
Vardges

Reputation: 197

url = new java.net.URL()

url = new java.net.URL(s) doesn't work for me.

I have a string C:\apache-tomcat-6.0.29\webapps\XEPServlet\files\m1.fo and need to make a link and give it to my formatter for output, but malformed url recieved. It seems that it doesn't make my string to url. I want also mention, that file m1.fo file is in files folder, in my webapp\product\, and I gave the full path to string like: getServletContext().getRealPath("files/m1.fo"). What I am doing wrong? How can I recieve the url link?

Upvotes: 0

Views: 9134

Answers (4)

Vardges
Vardges

Reputation: 197

It isn't preferable to write file:/// . Indeed it works on windows system,but in unix - there were problems. Instead of using

myReq.put("xml", new String []{"file:" + System.getProperty("file.separator") + 
                        getServletContext().getRealPath(DESTINATION_DIR_PATH) + 
                        System.getProperty("file.separator") + xmlfile}); 

you can write

myReq.put("xml", new String [] {getUploadedFileURL (xmlfile)} );

, where

public String getUploadedFileURL(String filename) {
    java.io.File filePath = new java.io.File(new 
            java.io.File(getServletContext().getRealPath(DESTINATION_DIR_PATH)), 
            filename);

    return filePath.toURI().toURL().toString();

Upvotes: 1

Martin Ždila
Martin Ždila

Reputation: 3219

Try: file:///C:/apache-tomcat-6.0.29/webapps/XEPServlet/files/m1.fo

Upvotes: 1

Jcs
Jcs

Reputation: 13709

It is possible to get an URL from a file path with the java.io.File API :

String path = "C:\\apache-tomcat-6.0.29\\webapps\\XEPServlet\\files\\m1.fo";
File f = new File(path);
URL url = f.toURI().toURL();

Upvotes: 5

Konstantin Komissarchik
Konstantin Komissarchik

Reputation: 29139

A file system path is not a URL. A URL is going to need a protocol prefix for one. To reference file system use "file:" in front of your path.

Upvotes: 0

Related Questions