2016-10-15 10 views
-2

私はこの機能の部分をサーバからpdfのようなファイルをダウンロードして新しいディレクトリに保存するはずです。それはこれを行いますが、空のpdfまたはテキストファイルです。修正する方法。空のファイルをダウンロードしていないと思われるコード

`File urlfile = new File(host + "/" + path); 
      urlfile.getParentFile().mkdirs(); 
      // create outputstream for request and inputstream for data 
      // download 

      FileOutputStream outS = new FileOutputStream(urlfile); 
      DataInputStream instream = new DataInputStream(newsocket.getInputStream()); 

      // get rid of head part to get to actual file 
      String l = null; 
      String lastmodtime = null; 
      boolean done = false; 
      while (!(l = DAA.readLine()).equals("")) { 

       if (!done && l.contains("Last-Modified:")) { 
        lastmodtime = l.substring(l.indexOf(' ') + 1, l.length()); 
        done = true; 
        System.out.println(l); 
       } 
      } 

      // read in bytes to correct file name 
      try { 
       byte[] inbytes = new byte[16384]; 
       int input; 
       while ((input = instream.read(inbytes)) != -1) { 
        outS.write(inbytes, 0, input); 

       } 
      }` 
+0

これはコンパイルされません。 – Tibrogargan

+0

大丈夫、バグはどこですか? – moalbait

+0

あなたは正しいです。それはtryとloopには入っていないので、何も書かれません。あなたは何をすればいいのか教えてください。 – moalbait

答えて

1

ファイルのコピーを作成したいか、あなたもJavaのコピーファイル操作用のApache CommonsのIO(FileUtils.copyFile(source, dest))を使用することができる場合は、この単純なコードを試すことができます。

private static void copyFileUsingStream(File source, File dest) 
      throws IOException { 
     InputStream is = null; 
     OutputStream os = null; 
     try { 
      is = new FileInputStream(source); 
      os = new FileOutputStream(dest); 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = is.read(buffer)) > 0) { 
       os.write(buffer, 0, length); 
      } 
     } finally { 
      is.close(); 
      os.close(); 
     } 
    } 
関連する問題