开发者

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 ur开发者_开发百科l. 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();


Try: 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜