简体   繁体   中英

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?

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();

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

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM