-1

I have this :

fos = new FileOutputStream(new File(cdn.replace('/', File.separatorChar), request.getEntity().getNewfilepath()));

I also tried this:

fos = new FileOutputStream(new File(cdn, request.getEntity().getNewfilepath()));

But I'm getting an error:

java.io.FileNotFoundException: http:\cdn\test.jpg (The filename, directory name, or volume label syntax is incorrect)

Any suggesion how can i fix this??

cdn is url :http://cdn

What I'm trying to achieve is to save the file on http://cdn/test.jpg

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
None
  • 8,817
  • 26
  • 96
  • 171
  • 2
    Now that I think of it, if it's an http url why in the world are you replacing the slashes at all? Or why are you using an http url for output? – Federico klez Culloca Jul 29 '19 at 09:49
  • @FedericoklezCulloca cdn is url http://cdn – None Jul 29 '19 at 09:53
  • I want to save this file on file system that have url : http//cdn + fiilepath – None Jul 29 '19 at 09:54
  • This can't work. An http url is not a writable file. You can't write to it. – Federico klez Culloca Jul 29 '19 at 09:57
  • And if it's the name of an actual file on your filesystem, chances are it can't contain a `:` character. – Federico klez Culloca Jul 29 '19 at 09:58
  • Why is this working locally then? when i replace cdn with C:/Desktop it save it on desktop? – None Jul 29 '19 at 09:59
  • Because "C:" is a valid drive name, "http:" is not – Federico klez Culloca Jul 29 '19 at 09:59
  • so how can i fix this then? – None Jul 29 '19 at 10:00
  • What you have is a URL. This is only for HTTP protocol. You cannot simply save a file to such a URL.. Even if you are inside the server which exposes this file to the outside via HTTP. You have to use the path on your local file system to store the file. – vanje Jul 29 '19 at 10:02
  • 1
    If it is URL that means that it is treated as remote system (even though it could be local). In this case the remote server should provide some HTTP API for communicating with it. Lets say it provides an API for writing info and API for reading info. Then you send an HTTP request to the server that executes your request and responds with HTTP response. FileOutputStream works on local system and not on remote – Michael Gantman Jul 29 '19 at 10:03
  • I'm guessing English is not your first language and you are having difficulty stating your question. Are you trying to download and save a HTML page from the Internet to your local computer? Or are you trying to upload a file from your local computer to some Internet site? – Abra Jul 29 '19 at 10:04
  • @Abra i have url of some file. And i want to save that file on server. For example if i have http://google.com/test.jpg i want to save that file on http://cdn/opt/test.jpg so that user can have that file on this server location. – None Jul 29 '19 at 11:02
  • So you want to download a file from some Internet site and then upload it to another Internet site. Correct? – Abra Jul 29 '19 at 16:54
  • I want to download file from some internet site and save it on file system (http://cdn) – None Jul 30 '19 at 07:41

1 Answers1

3

Forward slash always works, also on Windows. See "forward-slash-or-backslash"

And backward slash will definitely not work on URLs.

After your response on Abra I understand better what you want to do. You need to open the URL as a inputstream and create a new outputstream wchich points to your local file.

File understands http layout so you can use it to get the last part of the url which contains the name of the file (see variable f):

File also has a constructor with 2 arguments: path + filename. If you use this one you don't need to bother forward or backward slash problems.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class InternetReader {

    private static void copyInputStreamToOutputstream(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    }

    public static void main(String[] args) throws IOException {
        File f = new File("http://google.be/test.jpg");
        System.out.println(f.getName());
        File localPath = new File("/cdn/opt");
        File localDestination = new File(localPath, f.getName());
        URL remoteURL = new URL("http://google.be/test.jpg");
        try (InputStream is = remoteURL.openStream(); OutputStream os = new FileOutputStream(localDestination)) {
            copyInputStreamToOutputstream(is, os);
        }
    }

}
Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • This will be fine for files on a local system, but since OP is trying to use an HTTP URL for file output the problems are deeper than which type of slash should be used. – jsheeran Jul 29 '19 at 09:53
  • @jsheeran Not deeper. Shallower. Non-existent in fact. There are no backslashes in URLs. – user207421 Jul 29 '19 at 09:55