0

I am Create a Program which is download file from server

public void downloadFile(String fileURL, String saveDir, String user, String pass, String FileName)
            throws IOException {
        String authString = user + ":" + pass;

        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);

        URL url = new URL(fileURL);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestProperty("Authorization", "Basic " + authStringEnc);
        int responseCode = httpConn.getResponseCode();

        // always check HTTP response code first
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String fileName = "";
            String disposition = httpConn.getHeaderField("Content-Disposition");
            String contentType = httpConn.getContentType();

            int contentLength = httpConn.getContentLength();

            if (disposition != null) {
                // extracts file name from header field
                int index = disposition.indexOf("filename=");
                if (index > 0) {
                    fileName = disposition.substring(index + 10,
                            disposition.length() - 1);
                }
            } else {
                // extracts file name from URL
                fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                        fileURL.length());
            }

            System.out.println("Content-Type = " + contentType);
            System.out.println("Content-Disposition = " + disposition);
            System.out.println("Content-Length = " + contentLength);
            System.out.println("fileName = " + fileName);

            // opens input stream from the HTTP connection
            InputStream inputStream = httpConn.getInputStream();
            String saveFilePath = saveDir + File.separator + fileName;

            // opens an output stream to save into file
            FileOutputStream outputStream = new FileOutputStream(saveFilePath);

            int bytesRead = -1;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();

            System.out.println("File downloaded");
        } else {
            System.out.println("No file to download. Server replied HTTP code: " + responseCode);
        }
        httpConn.disconnect();
    }

so when i am download file using parameters i faced a error the output is

Content-Type = application/http
Content-Disposition = null
Content-Length = 3217551
fileName = 13.25.00-13.26.00[R][0@0][0].dav

Exception in thread "main" java.io.FileNotFoundException: C:\Cam\13.25.00-13.26.00[R][0@0][0].dav
 (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
    at dynakode.utility.Downloader.downloadFile(Downloader.java:65)
    at dynakode.camera.DynakodeCamera.cennection(DynakodeCamera.java:50)
    at dynakode.camera.DynakodeCamera.main(DynakodeCamera.java:16)
Java Result: 1

as u can see Error is The filename, directory name, or volume label syntax is incorrect so how can i fix this

I am Change this line

  String saveFilePath = saveDir + "\\" + "test.dav";

i am giving name manually than file is download to driver

the output is

Content-Type = application/http
Content-Disposition = null
Content-Length = 3217551
fileName = 13.25.00-13.26.00[R][0@0][0].dav

File downloaded

which is i want

issue Fixed

@TAsk you are write there is issue of Space but at C:/cam .. the is at file name which comes from server there is space " 13.25.00-13.26.00[R][0@0][0].dav" which is create problem so i am trim all path

 saveFilePath = saveFilePath.trim();

using this an d issue fixed

Ashish
  • 5
  • 1
  • 4

2 Answers2

1

Your String has \13 in path which is considering it as a character representation.

C:\Cam\13.25.00-13.26.00[R][0@0][0].dav
      ^

You should change it to either / or \\

So change your saveFilePath

String saveFilePath = saveDir + "\\" + fileName;
String saveFilePath = saveDir + "/" + fileName;

But

This should not be the case here because of File.separator() (Pointed out by user2864740) so one more reason according to me can cause this problem which I believe is extra space before C: or somewhere in path.

" C:\Cam"+File.separator+"13.25.00-13.26.00[R][0@0][0].dav"
 ^//<-------Extra Space you can use string.trim() to remove extra spaces

EDIT

Than the last thing which I think is directory (Cam) is not present that could be the last option which can cause this.

akash
  • 22,664
  • 11
  • 59
  • 87
  • ohky how can i fix this – Ashish Aug 04 '14 at 07:18
  • I am getting same error – Ashish Aug 04 '14 at 07:22
  • 1
    I do not believe this is the problem. The error message gives the *actual* file name (so escapes in string literals need not apply) and the OP is using `String saveFilePath = saveDir + File.separator + fileName;`. – user2864740 Aug 04 '14 at 07:26
  • Same Error Not working – Ashish Aug 04 '14 at 07:27
  • You have o check .. my edited post .. space is not a issue – Ashish Aug 04 '14 at 07:43
  • 1
    @TAsk you are write there is issue of Space but at C:/cam .. the is at file name which comes from server there is space `" 13.25.00-13.26.00[R][0@0][0].dav"` which is create problem so i am trim all path saveFilePath = saveFilePath.trim(); using this an d issue fixed – Ashish Aug 04 '14 at 07:50
  • @Ashish Oh Finally!! 0_0 But I can's see any space in Exception. – akash Aug 04 '14 at 07:51
  • 1
    But in output after equals at fileName = 13.25.00-13.26.00[R][0@0][0].dav have a space bt at code i don't give any space and you can vate my post for my increase my reputation – Ashish Aug 04 '14 at 07:57
0

Because the path you specified is not the same as the path is it expecting so it is throwing the java.io.FileNotFoundException

Make sure you have created the cam directory under the c drive

SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • yes I created it already.. the file is download when i will give a manual name like number text any thing – Ashish Aug 04 '14 at 07:14
  • these are some basic Exception so instead of posting it over here a simple debugging or google search will do the job – SparkOn Aug 04 '14 at 07:15
  • This is an output file. It isn't 'expecting' anything. – user207421 Aug 04 '14 at 07:29
  • @EJP It is expecting the "the `cam` directory under the `c` drive". (Although the wording in the first sentence could be cleaned up.) – user2864740 Aug 04 '14 at 07:31
  • @EJP Ok in that case the output file must have some harry potter's wand to make the directory specified in the path because the OP never specified the code to make a directory if it doesnot exist – SparkOn Aug 04 '14 at 07:33