2016-11-07 8 views
0

次の問題があります。私はサーバーからpdfファイルをダウンロードしなければならず、その中には名前に空白があります。したがって、すべてのファイルがダウンロードされますが、空白を持つファイルは開くことができません。パス内の空白を含むダウンロード済みファイルが破損しています

私はこのファイルをChrome経由でサーバーにアクセスすると、それらはうまく開きます(URL内の空白も含む)。

私が疑問に思っていることは、javaはファイルがダウンロードされるということです。しかし、Acrobat Readerでそれらを開こうとすると、ファイルが壊れているというエラーメッセージが表示されます。私のコードのサンプルは次のとおりです:

public static void downloadFile(String fileURL, String saveDir) throws IOException { 
    Authenticator.setDefault(new Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() {   
      return new PasswordAuthentication("*****", "*********".toCharArray()); 
     } 
    }); 

    final int BUFFER_SIZE = 4096; 
    URL url = new URL(fileURL); 
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 
    String credentials = "ptt" + ":" + "ptt123"; 
    String encoding = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)); 
    httpConn.setRequestProperty("Authorization", String.format("Basic %s", encoding)); 
    int responseCode = 0; 
    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()); 
     } 



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

また、fileUrlの "%20"で空白を置き換えようとしました。 何が問題なのですか?上に書いたように、空白のないファイルは、問題なくダウンロードした後に開くことができます。

私はJava 1.7を使用します。

乾杯、

アンドレイ

+2

あなたのOSは何ですか? –

+0

私はデスクトップ上でWindows 7を使用していますが、どのOSがサーバー上にあるのかわかりません。 –

答えて

0

ファイル名にスペースが含まれている場合は、その後いくつかの他のcharecterにそれを交換してください。それはうまくいかない場合は私に知らせてください。

if(fileName.trim().contains(" ")) 
    fileName.replace(" ","_"); 
+0

あなたの答えをありがとう、私はあなたの解決策を試みたが、それは動作しません。 –

+0

あなたは春を使っていますか? –

+0

いいえ、全くフレームワークを使用していません –

関連する問題