2016-04-30 76 views
0

我有一個java類從服務器下載文件,此文件開始下載,也完成了下載,但它沒有給出它保存在本地系統的路徑,或我認爲它不保存該文件,請指導我如何將它保存在窗口的下載文件夾中或任何其他文件夾中。如何保存下載的文件從服務器在java

import java.io.*; 
    import java.net.*; 
    import java.util.*; 



    class Download extends Observable implements Runnable { 


    private static final int MAX_BUFFER_SIZE = 1024; 

    public static final String STATUSES[] = {"Downloading", 
     "Paused", "Complete", "Cancelled", "Error"}; 

    public static final int DOWNLOADING = 0; 
    public static final int PAUSED = 1; 
    public static final int COMPLETE = 2; 
    public static final int CANCELLED = 3; 
    public static final int ERROR = 4; 

    private final URL url; // download URL 
    private int size; // size of download in bytes 
    private int downloaded; // number of bytes downloaded 
    private int status; // current status of download 

    public Download(URL url) { 
     this.url = url; 
     size = -1; 
     downloaded = 0; 
     status = DOWNLOADING; 

     // Begin the download. 
     download(); 
    } 

    public String getUrl() { 
     return url.toString(); 
    } 

    public int getSize() { 
     return size; 
    } 

    public float getProgress() { 
     return ((float) downloaded/size) * 100; 
    } 

    public int getStatus() { 
     return status; 
    } 

    public void pause() { 
     status = PAUSED; 
     stateChanged(); 
    } 

    public void resume() { 
     status = DOWNLOADING; 
     stateChanged(); 
     download(); 
    } 

    public void cancel() { 
     status = CANCELLED; 
     stateChanged(); 
    } 

    private void error() { 
     status = ERROR; 
     stateChanged(); 
    } 

    private void download() { 
     Thread thread = new Thread(this); 
     thread.start(); 
    } 

    private String getFileName(URL url) { 
     String fileName = url.getFile(); 
     return fileName.substring(fileName.lastIndexOf('/') + 1); 
    } 

    public void run() { 
     RandomAccessFile file = null; 
     InputStream stream = null; 

     try { 
      // Open connection to URL. 
      HttpURLConnection connection 
        = (HttpURLConnection) url.openConnection(); 

      // Specify what portion of file to download. 
      connection.setRequestProperty("Range", 
        "bytes=" + downloaded + "-"); 

      // Connect to server. 
      connection.connect(); 

      // Make sure response code is in the 200 range. 
      if (connection.getResponseCode()/100 != 2) { 
       error(); 
      } 

      // Check for valid content length. 
      int contentLength = connection.getContentLength(); 
      if (contentLength < 1) { 
       error(); 
      } 

      /* Set the size for this download if it 
    hasn't been already set. */ 
      if (size == -1) { 
       size = contentLength; 
       stateChanged(); 
      } 

      // Open file and seek to the end of it. 
      file = new RandomAccessFile(getFileName(url), "rw"); 
      file.seek(downloaded); 

      stream = connection.getInputStream(); 
      while (status == DOWNLOADING) { 
       /* Size buffer according to how much of the 
     file is left to download. */ 
       byte buffer[]; 
       if (size - downloaded > MAX_BUFFER_SIZE) { 
        buffer = new byte[MAX_BUFFER_SIZE]; 
       } else { 
        buffer = new byte[size - downloaded]; 
       } 

       // Read from server into buffer. 
       int read = stream.read(buffer); 
       if (read == -1) { 
        break; 
       } 

       // Write buffer to file. 
       file.write(buffer, 0, read); 
       downloaded += read; 
       stateChanged(); 
      } 


      /* Change status to complete if this point was 
    reached because downloading has finished. */ 
      if (status == DOWNLOADING) { 
       status = COMPLETE; 
       stateChanged(); 
       System.err.println("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"); 
      } 
     } catch (Exception e) { 
      error(); 
     } finally { 
      // Close file. 
      if (file != null) { 
       try { 
        file.close(); 
       } catch (Exception e) { 
       } 
      } 

      // Close connection to server. 
      if (stream != null) { 
       try { 
        stream.close(); 
       } catch (Exception e) { 
       } 
      } 
     } 
    } 

// Notify observers that this download's status has changed. 
    private void stateChanged() { 
     setChanged(); 
     notifyObservers(); 
    } 
} 

回答

0

當您創建RandomAccessFile的(),第一個參數是文件的名稱和位置。嘗試打印absolutePath()並查看此文件駐留的位置。

+0

你能告訴我,我必須把這個代碼? – gurjeet

+0

嘗試創建新的RandomAccessFile(「c:\\ temp \\ myfile.txt」,「RW」),然後根據您的原始代碼將下載的內容寫入此文件。我可以看到你試圖匹配URL的文件名。所以如果是這樣的話,文件應該保存在你啓動應用程序的位置。再次,調用file.getAbsolutePath()應該顯示文件位置。 –

相關問題