0

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?

1
  • 1
    @davogotland -- it should NOT be expected to be at 100%. One of his questions has no answer, and some of them don't seem to have any clear winner for an answer. 0% is a disincentive, but don't punish someone for not having 100% or even 75%. Commented Jan 14, 2011 at 22:01

4 Answers 4

5

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();
Sign up to request clarification or add additional context in comments.

2 Comments

You're going to need to escape those backslashes.
You don't need backslashes in filenames at all in Java. Use /.
1

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

Comments

1

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

1 Comment

Ok +1 for me, but maybe the solution could be exposed more clearly, I guess.
0

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.

3 Comments

I set the path file:///C:/apache-tomcat-6.0.29/webapps/XEPServlet/files/m1.fo and also used this: java.net.URL url = null; try{ url = new java.net.URL(s); } catch(java.net.MalformedURLException e){ errors = true; errorReport += "malformed url: '"+s+"'\n"; } return url;
Hi. I have already started my webservice, but it formats the file with static path. Now I want to keep the same path, but take any file. My uploader gives the path like file:///C:\Documents and Settings\Administrator\Desktop\m1.fo Question is : How can I take just the name from this path- file:///C:\Documents and Settings\Administrator\Desktop\m1.fo ??? Thank you in advance
Take a look at the URL class javadoc. There are a number of methods that allow you to retrieve various parts of the URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.